Windows Forms

41.1 WinForms Basics

An event-driven desktop GUI framework (the classic VB experience). It works on .NET Framework and .NET 8+ (Windows only).

Designer: drag controls from the Toolbox, set Properties (F4), double-click to create event handlers.

Each form = Form1.vb (your code) + Form1.Designer.vb (generated, don't edit by hand) + Form1.resx (resources).

41.2 A Simple Form in Code

Public Class MainForm
    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text = "Customer Manager"
        cboCity.Items.AddRange({"Pune", "Mumbai", "Delhi"})
        cboCity.SelectedIndex = 0
        LoadCustomers()
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        If String.IsNullOrWhiteSpace(txtName.Text) Then
            MessageBox.Show("Please enter a name.", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            txtName.Focus()
            Return
        End If
        lstCustomers.Items.Add($"{txtName.Text} ({cboCity.Text})")
        txtName.Clear()
        lblStatus.Text = $"{lstCustomers.Items.Count} customers"
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        If lstCustomers.SelectedIndex = -1 Then Return
        If MessageBox.Show("Delete selected customer?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            lstCustomers.Items.RemoveAt(lstCustomers.SelectedIndex)
        End If
    End Sub

    Private Sub MainForm_FormClosing(sender As Object, e As F
…

41.3 Common Controls

TextBox, MaskedTextBox, RichTextBox, NumericUpDown, DateTimePicker, MonthCalendar, CheckBox, RadioButton, ComboBox, ListBox, CheckedListBox, TrackBar

Label, LinkLabel, PictureBox, ProgressBar, StatusStrip, ToolTip, WebView2

Panel, GroupBox, TabControl, SplitContainer, FlowLayoutPanel, TableLayoutPanel

DataGridView, ListView, TreeView, BindingSource, BindingNavigator

MenuStrip, ContextMenuStrip, ToolStrip OpenFileDialog, SaveFileDialog, FolderBrowserDialog, ColorDialog, FontDialog, PrintDialog

Timer, BackgroundWorker, NotifyIcon, ErrorProvider, ImageList, HelpProvider

41.4 Data Binding and DataGridView

Private ReadOnly _customers As New BindingList(Of Customer)
Private ReadOnly _bs As New BindingSource()

Private Sub SetupBinding()
    _bs.DataSource = _customers
    dgvCustomers.DataSource = _bs
    txtName.DataBindings.Add("Text", _bs, "Name", True, DataSourceUpdateMode.OnPropertyChanged)
    dgvCustomers.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    dgvCustomers.Columns("Id").Visible = False
End Sub

Private Sub dgvCustomers_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvCustomers.CellFormatting
    If dgvCustomers.Columns(e.ColumnIndex).Name = "Balance" AndAlso CDec(e.Value) < 0 Then
        e.CellStyle.ForeColor = Color.Red
    End If
End Sub

41.5 Dialogs, Multiple Forms and MDI

Using dlg As New OpenFileDialog With {.Filter = "Text files|*.txt|All files|*.*", .Title = "Open file"}
    If dlg.ShowDialog() = DialogResult.OK Then
        txtEditor.Text = File.ReadAllText(dlg.FileName)
    End If
End Using

Using settings As New SettingsForm()
    settings.Username = currentUser               ' pass data via properties
    If settings.ShowDialog(Me) = DialogResult.OK Then currentUser = settings.Username
End Using

Dim report As New ReportForm()
report.Show()                                      ' modeless
' MDI: Me.IsMdiContainer = True : child.MdiParent = Me : child.Show()

41.6 Validation, Layout and Responsiveness

Validating events + ErrorProvider, AcceptButton/CancelButton, TabIndex, Anchor/Dock for resizing, TableLayoutPanel for responsive layouts, high-DPI support (<ApplicationHighDpiMode> in .NET).

Keep the UI responsive: Async/Await (Module 30) or BackgroundWorker (legacy). Update controls only on the UI thread (Control.Invoke / BeginInvoke from background threads).

If lblStatus.InvokeRequired Then
    lblStatus.Invoke(Sub() lblStatus.Text = "Updated from worker")
Else
    lblStatus.Text = "Updated"
End If

41.7 Drawing, Printing and More

Third-party control suites: DevExpress, Telerik, Syncfusion, Infragistics, ComponentOne. Krypton Toolkit (free) for modern styling.

Private Sub pnlCanvas_Paint(sender As Object, e As PaintEventArgs) Handles pnlCanvas.Paint
    Dim g = e.Graphics
    g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    Using pen As New Pen(Color.Blue, 3), brush As New SolidBrush(Color.LightGreen)
        g.FillEllipse(brush, 20, 20, 100, 100)
        g.DrawRectangle(pen, 150, 20, 120, 80)
        g.DrawString("GDI+ Drawing", New Font("Segoe UI", 14), Brushes.Black, 20, 140)
    End Using
End Sub
' Printing: PrintDocument + PrintPage event, PrintPreviewDialog
' Charts: System.Windows.Forms.DataVisualization (Framework) or ScottPlot / LiveCharts (NuGet)