C# VS2026
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

  1. File → New → Project → Windows Forms App (select C#, not VB).
  2. Name it MyWinFormsApp and click Create.
  3. Visual Studio opens the designer showing a blank Form1.

Adding Controls

Open the Toolbox (View → Toolbox) and drag controls onto the form:

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.