Advanced 12 min read Control #28

ToolStrip Control

ToolStrip creates professional toolbars with buttons, dropdowns, and other controls. It's like a menu but with icons and quick actions.

What is a ToolStrip?

ToolStrip is a container for toolbar items. It can contain buttons, labels, separators, dropdowns, textboxes, and more. It's perfect for quick access to common actions.

Code Example

// Add ToolStrip in designer or code
ToolStrip toolStrip = new ToolStrip();

// Add items
ToolStripButton btnNew = new ToolStripButton();
btnNew.Text = "New";
btnNew.Image = Image.FromFile("new.png");
btnNew.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
btnNew.Click += btnNew_Click;
toolStrip.Items.Add(btnNew);

// Add separator
toolStrip.Items.Add(new ToolStripSeparator());

// Add dropdown button
ToolStripDropDownButton btnExport = new ToolStripDropDownButton();
btnExport.Text = "Export";
btnExport.DropDownItems.Add("PDF");
btnExport.DropDownItems.Add("Excel");
btnExport.DropDownItems.Add("Word");
toolStrip.Items.Add(btnExport);

// Add to form
this.Controls.Add(toolStrip);

// Use with ImageList
toolStrip.ImageList = imageList1;
btnNew.ImageIndex = 0;

Exercise

Task: Create a text editor with ToolStrip.

  1. Add a ToolStrip at the top.
  2. Add buttons: New, Open, Save, Cut, Copy, Paste.
  3. Add a separator and then Bold, Italic, Underline buttons.
  4. Add a ComboBox for font size.
  5. Add a ToolStripLabel showing the current position.
  6. Use ImageList for button icons.
Key Takeaway

ToolStrip adds professional toolbars to your app. Use it with ImageList for icons and add various item types for flexibility.