Click, TextChanged, Load and more in WinForms
Events are the heartbeat of a Windows Forms application. They tell your app when the user clicks a button, types in a box, or opens a form β and they let you respond with code.
Letβs explore the most useful events youβll use again and again.
π§° What Are Events?
In WinForms, events are triggers that respond to user actions or application changes. When an event fires, your attached method β the event handler β runs automatically.
π Getting Started
Every control in WinForms has its own set of events. You can:
- Double-click a control in the designer to auto-generate a handler
- Or connect one manually:
button1.Click += MyClickHandler;
π Click β Button Presses & More
One of the most common events. Fires when the user clicks a control like a Button, Label, or even a Form.
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Button was clicked!");
}
β Used for: Submitting forms, triggering actions, navigation
π TextChanged β Detecting Input Updates
Fires whenever the text inside a TextBox, RichTextBox, or similar control changes β including from code!
private void textBox1_TextChanged(object sender, EventArgs e)
{
label1.Text = "You typed: " + textBox1.Text;
}
β Used for: Live validation, search filters, character counters
πͺ Load β Run Code When the Form Starts
Fires once, when the form first loads. Ideal for setup code, default values, or pre-loading data.
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.AddRange(new string[] { "Red", "Green", "Blue" });
}
β Used for: Populating controls, setting initial values, reading from files or databases
β¨οΈ Other Common Events
| Event | Fired When… | Typical Use |
|---|---|---|
CheckedChanged | Checkbox or radio button is toggled | Settings toggles, form options |
SelectedIndexChanged | A new item is selected in a ComboBox | Update UI based on selection |
KeyDown / KeyPress | A key is pressed while a control is active | Keyboard shortcuts, input validation |
MouseEnter / MouseLeave | Cursor moves over or off a control | Tooltips, hover effects |
FormClosing | User closes the form | Confirm save, cleanup, autosave |
βοΈ Example: Live Feedback Form
private void textBoxName_TextChanged(object sender, EventArgs e)
{
labelGreeting.Text = "Hello, " + textBoxName.Text + "!";
}
private void buttonSubmit_Click(object sender, EventArgs e)
{
MessageBox.Show("Submitted: " + textBoxName.Text);
}
π‘ Add this to the formβs Load event:
labelGreeting.Text = "Type your name above.";
π§ͺ Quick Challenge
π§© Create a form with:
- 1
TextBoxfor the name - 1
Buttonto submit - 1
Labelto greet the user live
Expected behavior:
- Typing updates the greeting in real time
- Clicking the button shows a message box
π Summary
| Event | Description |
|---|---|
Click | Runs code when user clicks a control |
TextChanged | Detects text updates in input fields |
Load | Runs once when form is first shown |
FormClosing | Fires when app window is closing |
KeyPress | Runs on key input (with character info) |
π Whatβs Next?
- β Combine multiple events for smart forms
- β Use timers for live updates
- β
Handle file saving in
FormClosing - β
Trigger sounds or animations on
Click
π¬ Have a form you want to wire up with events?
Let me help you plan it out or troubleshoot your handlers!