How Events Work in a Windows Form Application

2022-06-21 18:06:15 By : Ms. Amy Wang

Add UI interactivity to your Windows Form app with flexible event handling.

Windows Forms allows you to create desktop applications visually, by dragging UI elements onto a canvas. These UI elements include widgets such as buttons, panels, or checkboxes.

Each UI element receives certain events. For example, you can have a click event for buttons, a changed event for checkboxes, or a drag and drop event for panels.

Events use event handlers or functions, which only execute when that particular event occurs.

Each UI element has a list of events. There are many courses where you can learn about important UX or UI theories and practices to help you decide what UI elements to use. Here are a few examples of events used by UI elements.

UI elements that allow the user to enter text, such as a text box, can use these events. These events get triggered every time the user presses a key on their keyboard.

These can be useful in scenarios where you have search functionality, and you may need to constantly check what the value of the text box is.

The load event occurs when the form or a UI element is rendered on the screen. You can use this event when you want specific functionality to occur at the form or control's initialization stage.

One scenario where this may be useful is if you want to programmatically add controls to the form while it is still loading.

The tooltip popup event happens when you hover over a UI element on the application, and a tooltip appears. The arguments passed into the event handler allow you to access data about the tooltip, such as its text or size.

Many UI elements can use the drag and drop event, including the panel, button, picture box, group box, and more. This event gets triggered when the user drags a file into the UI element.

The mouse over event fires when the mouse hovers over a UI element. Once the mouse leaves and stops hovering over the element, the mouse leave event gets triggered.

UI elements that allow the user to select an option can use the checked changed event. This includes radio buttons and checkboxes. The function gets triggered when you check or uncheck the checkbox.

The value changed event is available on UI elements that allow you to select an option to change a value. This includes combo boxes, date and time pickers, or the calendar. The function gets triggered when the user selects a new value.

The click event handler function gets triggered when you click a button.

Event handlers have two primary parameters: the sender and an event object.

The sender is a reference to the UI element or object that has triggered the event, such as a button, checkbox, or panel. For example, in a checkbox changed event, the sender would be the checkbox that the user clicked on.

​​The event parameter contains an object that stores data about the event that occurred. This could include the X and Y coordinates of a button click, or the location of the mouse at the moment the event triggers.

First, create a new Winforms Forms application in Visual Studio. If you are new to Windows Forms, there are many clone apps you can make while learning Windows Forms.

You can generate event handlers from the properties window on the right-hand side of the canvas. Once you have created a new Windows Forms application, create an event handler for the checkbox UI element. This will trigger when the user checks or un-checks the checkbox.

Create a new function in the code behind and link it to the UI element on the canvas.

You can link the same function to multiple events. In this case, if there was a single function called MouseEvent, you could add that event handler to both the mouse hover and mouse click event. This will then handle both events using the same function.

A Windows Forms application allows you to drag and drop various UI elements such as buttons, panels, or text boxes onto a canvas. You can add event handlers to these UI elements, based on the different events that can occur within the application.

If you are building a Windows Application, you may also want to add images or graphics as part of your user interface. You can use various C# classes such as the Graphic, Pen, or Color class, which will allow you to draw different types of shapes onto the canvas.

Sharlene is a Tech Writer at MUO and also works full time in Software Development. She has a Bachelor of IT and has previous experience in Quality Assurance and University tutoring. Sharlene loves gaming and playing the piano.

Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!