Handling Events in Forms
Learn how to handle user interactions like button clicks and form submissions.
Handling Events in Forms is a free C# Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
What Are Events?
Events are actions or occurrences that a program can respond to, such as a button click, mouse movement, or text input. Event handling allows you to define how your application should react to these actions.
Key Point: Windows Forms uses event-driven programming to respond to user actions.

2
What Are Events?
Events are actions or occurrences that a program can respond to, such as a button click, mouse movement, or text input. Event handling allows you to define how your application should react to these actions.
Key Point: Windows Forms uses event-driven programming to respond to user actions.
3
Subscribing to Events
To handle an event, you need to subscribe to it by assigning a method (event handler) that will be executed when the event occurs. Example:
using System;
using System.Windows.Forms;
class Program : Form {
static void Main() {
Application.Run(new Program());
}
public Program() {
Button button = new Button() {
Text = "Click Me",
Left = 100,
Top = 50
};
button.Click += Button_Click;
Controls.Add(button);
}
private void Button_Click(object sender, EventArgs e) {
MessageBox.Show("Button was clicked!");
}
}
4
Common Events
Here are some common events in Windows Forms:
- Click: Triggered when a button or other clickable control is clicked.
- TextChanged: Triggered when the text in a textbox changes.
- MouseEnter: Triggered when the mouse pointer enters a control.
- KeyDown: Triggered when a key is pressed while a control is focused.
Key Point: Each event has a specific purpose, and you can use multiple events to enhance interactivity.
5
Adding Event Handlers in the Designer
You can add event handlers using the Windows Forms Designer:
- Select the control in the Designer.
- Open the Properties window and go to the Events tab (lightning bolt icon).
- Double-click the desired event (e.g.,
Click) to generate an event handler in the code.
Tip: This approach is faster for adding event handlers to multiple controls.
6
Event Arguments
Event handlers often include an EventArgs parameter that provides additional information about the event. Example:
using System;
using System.Windows.Forms;
class Program : Form {
static void Main() {
Application.Run(new Program());
}
public Program() {
TextBox textBox = new TextBox() {
Left = 50,
Top = 50,
Width = 200
};
textBox.TextChanged += TextBox_TextChanged;
Controls.Add(textBox);
}
private void TextBox_TextChanged(object sender, EventArgs e) {
TextBox textBox = (TextBox)sender;
Console.WriteLine("Text changed to: " + textBox.Text);
}
}
8
Practice Challenge
Create a Windows Forms application with:
- A button that displays a message when clicked.
- A textbox that logs text changes to the console.
- A label that updates when the mouse pointer enters a specific area.
Example:
using System;
using System.Windows.Forms;
class Program : Form {
Label label;
static void Main() {
Application.Run(new Program());
}
public Program() {
Button button = new Button() {
Text = "Click Me",
Left = 50,
Top = 50
};
button.Click += (sender, e) => MessageBox.Show("Button clicked!");
Controls.Add(button);
TextBox textBox = new TextBox() {
Left = 50,
Top = 100,
Width = 200
};
textBox.TextChanged += (sender, e) => Console.WriteLine("Text changed to: " + textBox.Text);
Controls.Add(textBox);
label = new Label() {
Text = "Hover over me",
Left = 50,
Top = 150,
Width = 150
};
label.MouseEnter += Label_MouseEnter;
Controls.Add(label);
}
private void Label_MouseEnter(object sender, EventArgs e) {
label.Text = "Mouse is here!";
}
}
8
9
Best Practices for Event Handling
Here are some best practices:
- Use meaningful names for event handler methods.
- Avoid writing excessive logic directly in event handlers; call other methods instead.
- Detach event handlers when they are no longer needed to prevent memory leaks.
Tip: Test your event handlers to ensure they work as expected in all scenarios.
10
Great Job!
Congratulations! You’ve learned how to handle events in Windows Forms to make your applications interactive. In the next lesson, we’ll build a simple calculator application to apply what you’ve learned. Let’s keep coding!
Click Continue to move to the next lesson.

Frequently asked questions
Is the “Handling Events in Forms” lesson free?
Yes — the full text of “Handling Events in Forms” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Handling Events in Forms”?
Learn how to handle user interactions like button clicks and form submissions. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Handling Events in Forms” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Windows Forms
- Designing User Interfaces
- Handling Events in Forms