Lesson 27 of 30
Windows Forms GUI Programming
C# in Visual Studio 2026 — a hands-on guide for developers at every level.
Windows Forms Overview
Windows Forms (WinForms) is a mature UI framework for building desktop applications on Windows. You design forms visually using a drag-and-drop designer, then wire up events in C#.
Creating a WinForms Project
- File â New â Project â Windows Forms App (select C#, not VB).
- Name it
MyWinFormsAppand click Create. - Visual Studio opens the designer showing a blank
Form1.
Adding Controls
Open the Toolbox (View â Toolbox) and drag controls onto the form:
- Label â static text
- TextBox â user input
- Button â clickable action
- ListBox â list of items
- ComboBox â drop-down selector
Handling Events in Code
Double-click a button in the designer to auto-generate the click handler in Form1.cs:
private void btnGreet_Click(object sender, EventArgs e)
{
string name = txtName.Text;
if (string.IsNullOrWhiteSpace(name))
{
MessageBox.Show("Please enter a name.", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
lblOutput.Text = $"Hello, {name}!";
}
Building a Simple Calculator
private void btnAdd_Click(object sender, EventArgs e)
{
if (double.TryParse(txtA.Text, out double a) &&
double.TryParse(txtB.Text, out double b))
{
lblResult.Text = (a + b).ToString("F2");
}
else
{
MessageBox.Show("Enter valid numbers.");
}
}
âšī¸ WinForms vs WPF
WinForms is excellent for simple desktop tools and rapid development. For modern, data-bound, highly-styled desktop UIs, prefer WPF (Lesson 28) or .NET MAUI for cross-platform.