Hello Ivan -
e.Button represents the button on the toolbar that was clicked. You get
one click event for the entire toolbar, not one click event for each button
on the toolbar. The Button property of the ToolBarButtonClickEventArgs in
the click event gives that extra bit of information you need to figure out
which of the toolbar buttons was pressed.
Here's a solution that uses a delegate and avoids the Select statement.
With the Select statement, you run the risk of leaving out one of the
cases. On the other hand, but buttons are added in code, and not in the
designer. The code can be copied directly into a Form class. Then use the
Form_Load event to call the MakeButtons method.
-Robin
VB Team
Delegate Sub ExecuteSub()
Structure ButtonStruct
Sub New(ByVal n As String, ByVal t As String, ByVal e As ExecuteSub)
Name = n
Tip = t
Execute = e
End Sub
Dim Name As String
Dim Tip As String
Dim Execute As ExecuteSub
End Structure
Dim Buttons() As ButtonStruct = { _
New ButtonStruct("Open ", "Open a file", AddressOf DoOpen), _
New ButtonStruct("Save ", "Save", AddressOf DoSave), _
New ButtonStruct("Print ", "Print", AddressOf DoPrint), _
}
Private Sub MakeButtons()
ToolBar1.Appearance = ToolBarAppearance.Normal
ToolBar1.ButtonSize = New Size(ToolBar1.Width / (UBound(Buttons) +
1), ToolBar1.ButtonSize.Height)
Dim bs As ButtonStruct
For Each bs In Buttons
Dim b As New ToolBarButton(bs.Name)
b.ToolTipText = bs.Tip
ToolBar1.Buttons.Add(b)
Next
End Sub
Public Sub DoSave()
' Add code here
End Sub
Public Sub DoPrint()
' Add code here
End Sub
Private Sub DoOpen()
' Add code here
End Sub
Private Sub ToolBar1_ButtonClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.ToolBarButtonClickEventArgs) Handles
ToolBar1.ButtonClick
Buttons(ToolBar1.Buttons.IndexOf(e.Button)).Execute()
End Sub
--------------------