ToolStrip is a container for ToolStripItem elements. Each individual element on the ToolStrip is a ToolStripItem that manages the layout and event model for the type it contains. The ToolStrip controls provide a common interface for Menus and Strips in Windows Forms.
How to use ToolStrip Control
Drag and drop ToolStrip Control from toolbox on the window Form.
Add ToolStrip Item which you want to show. Add one of the items in your ToolStrip that derives from ToolStrip Item.
You can also add standard item through smart tag.
Here SplitButton is added
Enter text which you want to show as Menu.
Create event handlers for the above menu items (New, Open, Save, Exit) on the MenuStrip through double click on them.
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
}
Change the Form to an MDI Form.
private void frmToolStrip_Load(object sender, EventArgs e)
{
this.IsMdiContainer = true;}
Write code on the Click event of the New menu item.
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
Form frm = new Form();
frm.MdiParent = this;
frm.Text = "New";
frm.Show();
}
Run the project
New child Form will open when New menu item clicked.
Write code on the Click event of the Open menu item.
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
}
OpenFileDialog will open when Open menu item clicked.
Write code on the Click event of the Save menu item.
private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
// open savefile dialog
sfd.ShowDialog();
}
SaveFileDialog will open when Save menuitem clicked.
Write code on the Click event of the Exit menu item.
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
// close the window
this.Close();
}
ToolStrip Properties
Size: Set the height and width of the control.
Text: Set the text associated with this control.
RenderMode: Set a value that indicates which visual styles will be applied to the ToolStrip.
LayoutStyle: Set a value indicating how the ToolStrip lays out the items collection.
Leave Comment