Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 36 ยท Windows Forms

Windows Forms Overview

Understand VB.NET Windows Forms - the Form class, designer files, partial classes, Application.Run and the message loop, project setup on modern .NET, with read-only VB and runnable C# stand-ins.

Windows Forms (WinForms) is the classic .NET framework for desktop applications: windows, buttons, text boxes, grids, menus. It is the home turf of Visual Basic, where dragging a button onto a form and double-clicking it to write the click handler made desktop programming accessible to millions. WinForms is still fully supported on modern .NET (it runs on .NET 8, 9 and 10, on Windows only).

Honest limits. Windows Forms needs Windows, a GUI session and the VB.NET compiler. None of those exist in this site's sandbox. All VB code in this chapter is read-only, and no screenshots or window output are claimed. Where something can run, it is a clearly labelled C# stand-in that models the underlying idea (events, the message loop, layout arithmetic, binding) with ordinary console code.

The smallest WinForms program

VB
Imports System.Windows.Forms

Public Class MainForm
    Inherits Form

    Public Sub New()
        Me.Text = "Hello"
        Me.Width = 320
        Me.Height = 200
    End Sub
End Class

Module Program
    <STAThread>
    Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New MainForm())
    End Sub
End Module

A window is just a class that inherits Form. Its properties (Text, Width, BackColor) configure it; its methods and events give it behavior. Application.Run(form) shows the window and keeps the program alive until the form closes. <STAThread> marks the main thread as a single-threaded apartment, which Windows UI components require.

The message loop

Windows delivers input (mouse moves, key presses, paint requests, timers) as messages placed on a queue belonging to the UI thread. Application.Run starts a loop that takes a message, dispatches it to the right control, which raises a .NET event (Click, KeyDown, Paint), and your handler runs. When the handler returns, the loop takes the next message.

flowchart LR
    A["Mouse and keyboard"] --> B["Message queue"]
    B --> C["Message loop (Application.Run)"]
    C --> D["Control raises event"]
    D --> E["Your handler runs"]
    E --> C

Everything in a WinForms app happens one message at a time on the UI thread.

The consequence matters: while your handler runs, the loop is stalled. A handler that works for five seconds freezes the window for five seconds. You will fix that with Async/Await and background work in the project lessons.

C# A message loop in 30 lines (C# stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Designer files and partial classes

When you add a form in Visual Studio you get two files that together form one class:

  • MainForm.vb, your code: handlers and logic.
  • MainForm.Designer.vb, generated code: it creates each control, sets properties and adds it to Me.Controls inside InitializeComponent.
VB
' MainForm.Designer.vb (generated; do not edit by hand)
Partial Class MainForm
    Inherits System.Windows.Forms.Form
    Friend WithEvents btnSave As System.Windows.Forms.Button

    Private Sub InitializeComponent()
        Me.btnSave = New System.Windows.Forms.Button()
        Me.btnSave.Text = "Save"
        Me.btnSave.Location = New System.Drawing.Point(24, 24)
        Me.Controls.Add(Me.btnSave)
    End Sub
End Class

Partial Class lets the compiler stitch the files together. The designer rewrites its file whenever you change the layout, so keep your own code in the other file.

The form lifecycle

Event When
Load Before the form is shown the first time: set up data here
Shown After it is first displayed
Activated / Deactivate Gets or loses focus
FormClosing The user or code is closing it; set e.Cancel = True to stop it
FormClosed It is gone; release resources
C# Veto a close, like FormClosing (C# stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Modern .NET project file

On .NET 8 and later a WinForms VB project is a tiny SDK-style file:

XML
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <StartupObject>Sub Main</StartupObject>
  </PropertyGroup>
</Project>

WinExe means "a GUI program with no console window", and the -windows target framework moniker unlocks the Windows-only libraries. The older .NET Framework 4.8 projects use a different, longer project format; see Migrating to modern .NET.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which method starts the message loop of a WinForms app?

    Application.Run shows the form and processes messages until it closes.
  2. 2. Why does a long-running Click handler freeze the window?

    Handlers run on the UI thread, the same thread that runs the message loop.
  3. 3. Where does the designer put generated control-creation code?

    A partial class split: your code in one file, generated code in the Designer file.
  4. 4. How do you stop a form from closing in FormClosing?

    FormClosingEventArgs.Cancel = True cancels the close.

Summary

  • A WinForms window is a class that inherits Form; Application.Run starts the message loop.
  • Input becomes messages, then control events, then your handlers, one at a time on the UI thread.
  • The designer generates InitializeComponent in a partial class file; keep your logic in your own file.
  • Modern WinForms targets net8.0-windows (or later) and runs on Windows only.

Next: Controls and Events