Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 36 ยท Windows Forms

Layout, Dialogs and Menus

Learn WinForms layout in VB.NET - Anchor, Dock, TableLayoutPanel, FlowLayoutPanel, MessageBox, OpenFileDialog, custom dialogs with DialogResult, MenuStrip and StatusStrip - with runnable C# stand-ins.

A window that looks right at 800 by 600 and falls apart when the user maximizes it is a bad window. WinForms gives you layout tools that react to resizing, standard dialogs for common questions, and strip controls for menus, toolbars and status bars. This lesson shows the VB code (read-only) and models the logic with runnable C# stand-ins.

No GUI exists in this sandbox, so nothing here draws a window. The VB blocks are read-only; the C# stand-ins perform the same arithmetic and control flow that the real controls do, and say so in their comments.

Anchor and Dock

Every control has two layout properties.

  • Anchor pins edges to the parent. A text box anchored Top, Left, Right keeps its margins and stretches when the form widens. The default is Top, Left.
  • Dock glues the control to a whole edge (Top, Bottom, Left, Right) or fills the remaining space (Fill). A docked MenuStrip stays on top; a DataGridView docked Fill takes everything left over.
VB
txtSearch.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
grid.Dock = DockStyle.Fill
btnOk.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right

With an anchor on both Left and Right, the control's width changes by the same amount as the form's width. The stand-in computes that.

C# What Anchor does when the form grows (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.

Container controls

Real forms use containers to arrange many controls.

Container Layout
Panel, GroupBox A box that groups controls and can be docked
FlowLayoutPanel Flows children left to right, wrapping like text
TableLayoutPanel A grid of rows and columns with absolute, percent or auto sizes
SplitContainer Two panes with a draggable divider
TabControl Tabbed pages

A common pattern is a TableLayoutPanel with "Auto" label column and a "100%" input column; inputs then line up and resize without any manual math.

Message boxes and standard dialogs

MessageBox.Show asks a simple question and returns a DialogResult:

VB
Dim answer = MessageBox.Show("Delete this file?", "Confirm",
                             MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
If answer = DialogResult.Yes Then
    ' delete
End If

Other ready-made dialogs: OpenFileDialog, SaveFileDialog, FolderBrowserDialog, ColorDialog, FontDialog, PrintDialog.

VB
Using dlg As New OpenFileDialog()
    dlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
    If dlg.ShowDialog() = DialogResult.OK Then
        txtPath.Text = dlg.FileName
    End If
End Using

Always wrap dialogs in Using; they hold unmanaged resources.

Your own modal dialog

Create a second form, set AcceptButton and CancelButton, give the buttons a DialogResult, and show it with ShowDialog. The caller blocks until it closes, then reads its properties.

VB
Using dlg As New SettingsForm()
    dlg.UserName = currentName
    If dlg.ShowDialog(Me) = DialogResult.OK Then
        currentName = dlg.UserName
    End If
End Using

ShowDialog is modal: the owner is disabled until the dialog closes. Show is modeless: both windows stay usable. Prefer a modal dialog when the answer is required to continue.

C# The modal-dialog contract (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.

  • MenuStrip holds ToolStripMenuItems; each raises Click. Set ShortcutKeys (for example Keys.Control Or Keys.S) and ShowShortcutKeys.
  • ToolStrip holds buttons, separators and drop-downs for the same actions as the menu.
  • StatusStrip shows state: a ToolStripStatusLabel for text and a ToolStripProgressBar.
  • ContextMenuStrip is the right-click menu; assign it to a control's ContextMenuStrip property.

Menu item, toolbar button and shortcut should all call the same method, so each action exists once:

VB
Private Sub SaveDocument()
    ' the real work
    tslStatus.Text = "Saved at " & DateTime.Now.ToShortTimeString()
End Sub

Private Sub mnuSave_Click(sender As Object, e As EventArgs) Handles mnuSave.Click, tsbSave.Click
    SaveDocument()
End Sub
C# One command, many triggers (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.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A TextBox is anchored Top, Left, Right. What happens when the form gets wider?

    Anchoring both Left and Right keeps both margins, so the width changes.
  2. 2. What does DockStyle.Fill do?

    Fill expands the control into the space left by other docked controls.
  3. 3. Which call blocks the caller until the dialog closes?

    ShowDialog is modal and returns a DialogResult.
  4. 4. Why share one method between a menu item, toolbar button and shortcut?

    Single-source commands avoid duplicated, diverging code.

Summary

  • Anchor pins edges, Dock fills edges; containers like TableLayoutPanel arrange groups of controls.
  • MessageBox and the common dialogs return a DialogResult; wrap dialogs in Using.
  • Custom dialogs use ShowDialog (modal) and commit changes only on OK.
  • Menus, toolbars and shortcuts should call one shared method.

Next: Data Binding and Validation