C# Windows Forms ToolStrip Examples

Learn ToolStrip in C# using these simple step by step examples.

What is ToolStrip?

ToolStrip Provides a container for Windows toolbar objects.

public class ToolStrip : System.Windows.Forms.ScrollableControl, IDisposable

Here is its inheritance chain:

Object -> MarshalByRefObject -> Component -> Control -> ScrollableControl -> ToolStrip

ToolStrip is the base class for MenuStrip, StatusStrip, and ContextMenuStrip.

Use ToolStrip and its associated classes in new Windows Forms applications to create toolbars that can have a Windows XP, Office, Internet Explorer, or custom appearance and behavior, all with or without themes, and with support for overflow and run-time item reordering. ToolStrip controls also offer a rich design-time experience that includes in-place activation and editing, custom layout, and sharing of horizontal or vertical space within a specified ToolStripContainer.

The ToolStrip class provides many members that manage painting, mouse and keyboard input, and drag-and-drop functionality. Use the ToolStripRenderer class with the ToolStripManager class to gain even more control and customizability over the painting and layout style of all ToolStrip controls on a Windows Form.

The following items are specifically designed to work seamlessly with both ToolStripSystemRenderer and ToolStripProfessionalRenderer in all orientations. They are available by default at design time for the ToolStrip control:

  1. ToolStripButton
  2. ToolStripSeparator
  3. ToolStripLabel
  4. ToolStripDropDownButton
  5. ToolStripSplitButton
  6. ToolStripTextBox
  7. ToolStripComboBox

You can join ToolStrip controls within a specified ToolStripContainer and merge ToolStrip controls with each other. Use ToolStripPanel rather than ToolStripContainer for Multiple Document Interface (MDI) applications. Typically, a ToolStrip does not participate in the tab order unless it is absolutely positioned rather than being docked or in a ToolStripPanel.

Use the ToolStripControlHost class to host any other Windows Forms control in a ToolStrip.

By default, the ToolStrip is double buffered, taking advantage of the OptimizedDoubleBuffer setting.

ToolStrip replaces and extends the ToolBar control.

Let us look at some examples.

Example 1 - ToolStrip and ToolStripContainer Example

The following code example demonstrates adding a ToolStripContainer and a ToolStrip to a Windows Forms, adding items to the ToolStrip, and adding the ToolStrip to the TopToolStripPanel of the ToolStripContainer.

Here is the full code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
    private ToolStripContainer toolStripContainer1;
    private ToolStrip toolStrip1;

    public Form1()
    {
        InitializeComponent();
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

    private void InitializeComponent()
    {
        toolStripContainer1 = new System.Windows.Forms.ToolStripContainer();
        toolStrip1 = new System.Windows.Forms.ToolStrip();
        // Add items to the ToolStrip.
        toolStrip1.Items.Add("One");
        toolStrip1.Items.Add("Two");
        toolStrip1.Items.Add("Three");
        // Add the ToolStrip to the top panel of the ToolStripContainer.
        toolStripContainer1.TopToolStripPanel.Controls.Add(toolStrip1);
        // Add the ToolStripContainer to the form.
        Controls.Add(toolStripContainer1);
    }
}

Reference

Find the API documentation here.