ToolStrip

http://www.visual-basic-tutorials.com/Tutorials/Controls/ToolStrip.html


This control displays aseriesof buttons, dropdowns, and other tools.
You canaccessthese tools quickly without navigating through a series of menus, so they are mostusefulfor performing frequently needed tasks.
Menusare moreappropriatefor commands that are needednot that often.
The followinglistshows thetypes of itemsthat a ToolStrip may contain:

·  ToolStripButton

·  ToolStripLabel

·  ToolStripSplitButton

·  ToolStripDropDownButton

·  ToolStripSeparator

·  ToolStripComboBox

·  ToolStripTextBox

·  ToolStripProgressBar

·  At runtime, you canaccessthe controls inside theItems collection, or you can refer to the tools directly by theirnames.

Private Sub ToolStrip1_Click(sender As Object, e As EventArgs) _
Handles ToolStrip1.Click
For Each item As ToolStripItem In ToolStrip1.Items
' Do something
Next
End Sub
Private Sub ToolStrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) _
Handles ToolStrip1.ItemClicked
If e.ClickedItem.Name = "NewToolStripButton" Then
' ToDo: open a new file
End If
End Sub

http://www.dotnetheaven.com/article/toolstrip-control-in-vb.net

Using Designer

To create a ToolStrip control at design-time, simply drag and drop a ToolStrip control from Toolbox onto a Form. By default, a ToolStrip control is docked at the top of the Form. We can change docking and other properties using the Tasks control as shown in Figure 1.


Figure 1

Next step is to set ToolStrip control properties. We can set properties by calling Properties window if a control using Right click on the control and selecting Properties menu item or simply hit F4. Figure 2 shows Properties window where I set BackColor to green and GripStyle to Visible.


Figure 2

A ToolStrip control is nothing but a container without adding its child controls.A ToolStrip control is capable of hosting Button, Label, SplitButton, DropDownButton, Separator, ComboBox, TextBox and ProgressBar controls.

Now let's add a few controls to ToolStrip control.

If you click on little dropdown handle on a ToolStrip control in designer, you will see a list of possible controls (See Figure 3) that can be added to a ToolStrip. To add a control, simply select that control and it will be added to the ToolStrip control.


Figure 3

Another way to add controls to a ToolStrip control is by using the Items property.

Click on the Items property and you will see Item Collection Editor that looks like Figure 4. We are going to add a Button, a TextBox, and a Label control to ToolStrip with a few separators.


Figure 4

If you need to reposition controls, you can simply use up and down arrows.

Here is a good part. Once these controls are added to a ToolStrip control, the all act as individual controls and you can access them by using their Name property anywhere in the code.

Now let's create a sample. We are going to add three Button controls, three separators and a Label control as you can see in Figure 5.


Figure 5

Now next we are going to set an icon for the Button controls by setting Image property of Button. See Figure 6.


Figure 6

Once you click on the browse option to browse an image, Resource editor window pops up where you can import an image. Click on Import button and browse an image file.


Figure 7

We set three icons for all three buttons and the ToolStrip looks like Figure 8.


Figure 8

Next step is to set the button click event handler. It is easy. Just double click or use Events window and set click event handlers for all three buttons.


Figure 9

Three button click event handlers look like following where we set Text property of Label.

PrivateSubToolStripButton1_Click(ByValsenderAsSystem.Object, _

ByValeAsSystem.EventArgs)HandlesToolStripButton1.Click

ToolStripLabel1.Text ="Button1 is clicked"

EndSub

PrivateSubToolStripButton2_Click(ByValsenderAsSystem.Object, _

ByValeAsSystem.EventArgs)HandlesToolStripButton2.Click

ToolStripLabel1.Text ="Button2 is clicked"

EndSub

PrivateSubToolStripButton3_Click(ByValsenderAsSystem.Object, _

ByValeAsSystem.EventArgs)HandlesToolStripButton3.Click

ToolStripLabel1.Text ="Button3 is clicked"

EndSub

Now if you run the project and click on Buttons, you will see something like Figure 10.

Figure 10

How to: Add ToolStrip Items Dynamically

https://msdn.microsoft.com/en-us/library/ms229625(v=vs.110).aspx

Example

The following code example demonstrates how to dynamically add items to aContextMenuStripcontrol. The example also shows how to reuse the sameContextMenuStripfor three different controls on the form.

In the example, anOpeningevent handler populates the menu item collection. TheOpeningevent handler examines theContextMenuStrip.SourceControlandToolStripItem.OwnerItemproperties and adds aToolStripItemdescribing the source control.

Imports System

Imports System.Collections.Generic

Imports System.Windows.Forms

Imports System.Drawing

' This code example demonstrates how to handle the Opening event.

' It also demonstrates dynamic item addition and dynamic

' SourceControl determination with reuse.

Class Form3

Inherits Form

' Declare the ContextMenuStrip control.

Private fruitContextMenuStrip As ContextMenuStrip

Public Sub New()

' Create a new ContextMenuStrip control.

fruitContextMenuStrip = New ContextMenuStrip()

' Attach an event handler for the

' ContextMenuStrip control's Opening event.

AddHandler fruitContextMenuStrip.Opening, AddressOf cms_Opening

' Create a new ToolStrip control.

Dim ts As New ToolStrip()

' Create a ToolStripDropDownButton control and add it

' to the ToolStrip control's Items collections.

Dim fruitToolStripDropDownButton As New ToolStripDropDownButton("Fruit", Nothing, Nothing, "Fruit")

ts.Items.Add(fruitToolStripDropDownButton)

' Dock the ToolStrip control to the top of the form.

ts.Dock = DockStyle.Top

' Assign the ContextMenuStrip control as the

' ToolStripDropDownButton control's DropDown menu.

fruitToolStripDropDownButton.DropDown = fruitContextMenuStrip

' Create a new MenuStrip control and add a ToolStripMenuItem.

Dim ms As New MenuStrip()

Dim fruitToolStripMenuItem As New ToolStripMenuItem("Fruit", Nothing, Nothing, "Fruit")

ms.Items.Add(fruitToolStripMenuItem)

' Dock the MenuStrip control to the top of the form.

ms.Dock = DockStyle.Top

' Assign the MenuStrip control as the

' ToolStripMenuItem's DropDown menu.

fruitToolStripMenuItem.DropDown = fruitContextMenuStrip

' Assign the ContextMenuStrip to the form's

' ContextMenuStrip property.

Me.ContextMenuStrip = fruitContextMenuStrip

' Add the ToolStrip control to the Controls collection.

Me.Controls.Add(ts)

'Add a button to the form and assign its ContextMenuStrip.

Dim b As New Button()

b.Location = New System.Drawing.Point(60, 60)

Me.Controls.Add(b)

b.ContextMenuStrip = fruitContextMenuStrip

' Add the MenuStrip control last.

' This is important for correct placement in the z-order.

Me.Controls.Add(ms)

End Sub

' This event handler is invoked when the ContextMenuStrip

' control's Opening event is raised. It demonstrates

' dynamic item addition and dynamic SourceControl

' determination with reuse.

Sub cms_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

' Acquire references to the owning control and item.

Dim c As Control = fruitContextMenuStrip.SourceControl

Dim tsi As ToolStripDropDownItem = fruitContextMenuStrip.OwnerItem

' Clear the ContextMenuStrip control's

' Items collection.

fruitContextMenuStrip.Items.Clear()

' Check the source control first.

If (c IsNot Nothing) Then

' Add custom item (Form)

fruitContextMenuStrip.Items.Add(("Source: " + c.GetType().ToString()))

ElseIf (tsi IsNot Nothing) Then

' Add custom item (ToolStripDropDownButton or ToolStripMenuItem)

fruitContextMenuStrip.Items.Add(("Source: " + tsi.GetType().ToString()))

End If

' Populate the ContextMenuStrip control with its default items.

fruitContextMenuStrip.Items.Add("-")

fruitContextMenuStrip.Items.Add("Apples")

fruitContextMenuStrip.Items.Add("Oranges")

fruitContextMenuStrip.Items.Add("Pears")

' Set Cancel to false.

' It is optimized to true based on empty entry.

e.Cancel = False

End Sub

End Class