Toolboar

  • Thread starter Thread starter Ivan
  • Start date Start date
I

Ivan

How do I code a button in the toolbar in Visual
Basic .NET. I added a button but do not know how to code
that button to bring up another form.
 
* "Ivan said:
How do I code a button in the toolbar in Visual
Basic .NET. I added a button but do not know how to code
that button to bring up another form.

\\\
Private Sub ToolBar1_ButtonClick( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs _
) Handles ToolBar1.ButtonClick
Select Case True
Case e.Button Is Me.ToolBarButton1
 
I wanted to ask you a follow-up question and I am not sure if the
previous post went through. What does the e.Button actually
reprepsent/mean? The statement did work, though. Thank you very much.
 
That did work, thank you. Out of curiosity what is that e.Button
statement? What does the e represent?

Thanks again!
 
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



--------------------
 
Back
Top