How Events Work in a Windows Form Application

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.

4

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

Types of Events Used for Different UI Elements

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

Key Down, Key Up, or Key Press Events

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.

iCloud+ Website on MacBook Sitting on Kitchen Island

Load Event

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.

A photo of a television displaying the McMillions home page on HBO Max

Tooltip Popup Event

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.

Drag and Drop Event

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.

Mouse Over and Mouse Leave Events

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.

Checked Changed Event

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.

Value Changed, Selected Value Changed, or Date Changed Events

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.

Click Button Event

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

The Structure of an Event Handler

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.

A laptop on a desk showing Obsidian’s new Bases feature

​​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.

How to Create and Use Event Handlers

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

Event Handlers on the Canvas

it’s possible to 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.

Event Handlers Using the Code Behind

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

How to Use the Same Event Handler Across Multiple Events

You can link the same function to multiple events. In this case, if there was a single function calledMouseEvent, 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.

Using Events in a Windows Form Application

A Windows Forms application allows you to drag and drop various UI elements such as buttons, panels, or text boxes onto a canvas. you could 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 could 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.

A coffee cup sits behind some tax forms on a table

See how you can draw shapes, use colors, and render images in a WinForms app.

confirm you don’t miss these movies and shows before Netflix removes them.

The key is not to spook your friends with over-the-top shenanigans.

It saves me hours and keeps my sanity intact.

Lose your laptop without this feature, and you’ll wish you had turned it on.

Now, I actually finish the books I start.

Technology Explained

PC & Mobile