21
Lesson 21 of 35 · WinForms

WinForms UI Design

Windows Forms (WinForms) is the classic Windows GUI framework built into .NET. It uses a drag-and-drop designer inside Visual Studio 2026 to build event-driven desktop applications quickly.

Creating a WinForms Project

Go to File → New → Project and select Windows Forms App (.NET). Visual Studio creates a solution with Form1.cs (code), Form1.Designer.cs (auto-generated layout), and Program.cs (entry point).

WinForms entry point Program.cs
// Program.cs — WinForms entry point
ApplicationConfiguration.Initialize();
Application.Run(new Form1());

The Form Designer

Double-click Form1.cs to open the Design view. Open the Toolbox (View → Toolbox) and drag controls—Button, TextBox, Label, etc.—onto the form. Use the Properties panel to change names, text, colours, and fonts.

Setting Form Properties

The most important Form properties: Text (title bar caption), Size, FormBorderStyle, StartPosition, BackColor, and Icon. Set MaximizeBox = false to prevent resizing.

Form setup code FormSetup.cs
// In Form1 constructor (or Form1.Designer.cs)
this.Text            = "My First WinForms App";
this.Size            = new Size(600, 400);
this.StartPosition   = FormStartPosition.CenterScreen;
this.BackColor       = Color.FromArgb(15, 15, 30);
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox     = false;

A Complete Mini App

Let's build a temperature converter. Drag a Label, a TextBox, two RadioButtons, and a Button onto the form. Double-click the button to generate a Click event handler.

Temperature converter TempConverter.cs
private void btnConvert_Click(object sender, EventArgs e)
{
    if (!double.TryParse(txtInput.Text, out double val))
    {
        lblResult.Text = "Please enter a valid number.";
        return;
    }

    double result = rbCtoF.Checked
        ? val * 9 / 5 + 32
        : (val - 32) * 5 / 9;

    string from = rbCtoF.Checked ? "°C" : "°F";
    string to   = rbCtoF.Checked ? "°F" : "°C";
    lblResult.Text = $"{val}{from} = {result:F2}{to}";
}