ToolStrip and StatusStrip

The menu bar of the last lessons is one ToolStrip; a window usually has three. A toolbar of buttons under the menu, a status bar at the bottom with the counts and the state, and the menu itself. They are the same class in three roles, and the same item types appear in all of them.

A bar of buttons

using System; using System.Drawing; using System.Windows.Forms; internal static class Program { [STAThread] private static void Main() { ApplicationConfiguration.Initialize(); Application.Run(new Form1()); } } internal sealed class Form1 : Form { private readonly RichTextBox _editor = new() { Dock = DockStyle.Fill }; public Form1() { Text = "A toolbar"; ClientSize = new Size(560, 340); var bar = new ToolStrip { GripStyle = ToolStripGripStyle.Hidden, ImageScalingSize = new Size(16, 16), }; var fresh = new ToolStripButton("New", null, (sender, e) => _editor.Clear()) { ToolTipText = "A new note", DisplayStyle = ToolStripItemDisplayStyle.Text, }; bar.Items.Add(fresh); bar.Items.Add(new ToolStripButton("Save", null, (sender, e) => Text = "saved")); bar.Items.Add(new ToolStripSeparator()); var size = new ToolStripDropDownButton("Size"); size.DropDownItems.Add("Small", null, (sender, e) => _editor.Font = new Font("Segoe UI", 9F)); size.DropDownItems.Add("Large", null, (sender, e) => _editor.Font = new Font("Segoe UI", 14F)); bar.Items.Add(size); var find = new ToolStripTextBox { Width = 140, ToolTipText = "Search the note" }; bar.Items.Add(find); Controls.Add(_editor); Controls.Add(bar); } }

dotnet build app.csproj

Build succeeded.
    0 Warning(s)
    0 Error(s)

The listing is built and not run. The bar docks itself to the top of the form, above the editor only because it was added after it. DisplayStyle is a property of each item, not of the bar: ToolStripItemDisplayStyle.Text shows the caption alone, Image the image alone, and ImageAndText both, which is only readable with a small image. GripStyle.Hidden removes the drag handle at the left, which a docked toolbar does not need.

The item types

An item is not a control: it has no Location and no Controls. Its Text, Image, Enabled and ToolTipText work the way a control's do, and its Alignment moves it within the bar rather than within a container.

StatusStrip

A StatusStrip docks to the bottom and holds the labels of a status line. Its one property that needs explaining is Spring: a label with Spring = true takes all the free width, so the items after it are pushed to the right edge.

using System; using System.Drawing; using System.Windows.Forms; internal sealed class Form1 : Form { private readonly ToolStripStatusLabel _left = new("3 notes") { Spring = true, TextAlign = ContentAlignment.MiddleLeft }; private readonly ToolStripStatusLabel _state = new("Saved 09:41"); public Form1() { Text = "A status line"; ClientSize = new Size(520, 200); var status = new StatusStrip(); status.Items.Add(_left); status.Items.Add(new ToolStripStatusLabel { Text = " | " }); status.Items.Add(_state); Controls.Add(status); } }

That block is a fragment: a form with no Main, so the checker counts it and does not build it. The left label springs and the right one does not, which is the whole layout of a status bar: the message on the left, the state on the right, and the free space between them. BorderSides and SizingGrip are the two cosmetic properties worth setting: SizingGrip = false on a form that cannot be resized.

A status bar is a place for facts that change while the user works: the number of selected rows, the position of the caret, whether the document is saved, the progress of a job. It is not a place for an error message, which needs the user's attention and disappears from a status line unnoticed.

A status line that runs

The text of a status bar is computed from the document, and that computation runs here:

using System; using System.Linq; var text = "Notes on WinForms\nA form owns its controls.\nA form also owns its layout."; Console.WriteLine(Describe(text, 24)); Console.WriteLine(Describe(text, 0)); static string Describe(string text, int caret) { var before = text[..Math.Clamp(caret, 0, text.Length)]; var line = before.Count(c => c == '\n') + 1; var column = before.Length - before.LastIndexOf('\n'); var words = text.Split(new[] { ' ', '\n', '\r', '\t' }, StringSplitOptions.RemoveEmptyEntries).Length; var lines = text.Count(c => c == '\n') + 1; return $"words: {words} | lines: {lines} | Ln {line}, Col {column}"; }

dotnet run

words: 14 | lines: 3 | Ln 2, Col 7
words: 14 | lines: 3 | Ln 1, Col 1

The output is the real run. The caret at 24 is six characters into the second line, which the subtraction before.Length - before.LastIndexOf('\n') reports as column 7 because the first column is 1; the caret at 0 is the beginning of the document. The word and line counts use the whole text and do not depend on the caret, which is the split a status bar wants: two labels, one per fact, each updated when its fact changes.

Exercises

  1. Add a third fact (characters) to Describe and run the program.
  2. Change the column to be zero-based and run it again. Which number changes?
  3. Explain from the documentation what ToolStripSplitButton.Click and ButtonClick are.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which class is the toolbar of a window?

    Answer

    ToolStrip

    All three bars derive from ToolStrip; StatusStrip and MenuStrip are specialised.

  2. Which item shows text that is not a command?

    Answer

    ToolStripLabel

    A label has no click and no state; a button is a command.

  3. What does GripStyle.Hidden do?

    Answer

    Removes the drag handle at the left of the bar

    A docked toolbar does not need the handle.

  4. What does an item's DisplayStyle control?

    Answer

    Whether that item shows an image, a text or both

    DisplayStyle belongs to ToolStripItem, not to the bar, so it is set per item.

  5. Which item is a button and an arrow with two targets?

    Answer

    ToolStripSplitButton

    The button runs the last command; the arrow opens the menu.

  6. What does Spring = true on a status label do?

    Answer

    Takes the free width and pushes the later items right

    It is how a message on the left and a state on the right share one bar.

  7. Where does a StatusStrip dock?

    Answer

    The bottom, by default

    MenuStrip docks top and StatusStrip bottom without a line of code.

  8. What did the run print for the caret at 24?

    Answer

    Ln 2, Col 7

    Six characters into the second line, and the first column is 1.

  9. Which item puts a progress bar in a status bar?

    Answer

    ToolStripProgressBar

    An item is not a control, so the bar has an item type of its own.

  10. Why is a status bar a bad place for an error message?

    Answer

    It needs attention and a status line is unnoticed

    Errors belong where the user must see them: a dialog, or an error icon on the field.

Every listing on this page was built with the .NET SDK 10.0.303 for net10.0-windows against the targeting pack Microsoft.WindowsDesktop.App.Ref 10.0.15 from the offline feed, in a Docker container on Alpine Linux 3.24.3 (x86_64). The programs were built, not run on Linux: WinForms needs Windows. The logic that draws nothing was also run headless on the same machine, and its output is the run output printed below each of those programs.

Article author: Arthur Isaev