Anyone know how to call the Toolbar_Click event from code?
I've code to handle the basic user clicks (from the form) but want to fire the code behind a toolbar button ("Save") through code.
Thanks,
O.
Printable View
Anyone know how to call the Toolbar_Click event from code?
I've code to handle the basic user clicks (from the form) but want to fire the code behind a toolbar button ("Save") through code.
Thanks,
O.
Assuming that your toolbar is called 'ToolBar':VB Code:
Private Sub ToolBar_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar.ButtonClick Select Case ToolBar.Buttons.IndexOf(e.Button) Case 0 ' The first button MsgBox("You pressed the new button") Case 1 ' The second button MsgBox("You pressed the save button") End Select End Sub
Thanks, but that's the code to handle the event. I'm trying to call the ButtonClick event of the toolbar directly from code. In VB6 one might do something like so:
Call Command1_click
Looking for whatever .NET format allows me to do this with the toolbar.
O.
You do it the same as in VB6 only you have to pass the proper parameters along too.
VB Code:
Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick 'handle click event MsgBox(String.Concat("You pushed button # ", ToolBar1.Buttons.IndexOf(e.Button))) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'FIRE click event ToolBar1_ButtonClick(ToolBar1, New ToolBarButtonClickEventArgs(ToolBarButton1)) End Sub
Excellent, thank you Edneeis I'll give it a shot.
It was the parameter format I couldn't figure out.
O.
Alright its:
SubThatHandlesEvent(ToolbarControl, New ToolBarButtonClickEventArgs(ToolbarButtonObject)
Again, thank you very much. Just tested your advice and it worked perfectly. Thanks for the help.
O.
Just out or curiosity, where did you find information on passing the New ToolBarButtonClickEventArgs(buttonobject)?
O.
In the declaration of the event in the sub that handles it:
Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick
Most EventArgs Inherited object just accept the added parts into the constructor and the button is the added part. I do the samething when I'm making objects and have to add to the eventargs. I think it just takes a little getting used to the new 'system' for events, but once you do it makes things much easier having everything fairly standardized.
You can also use the Help or Object Browser to check out the custom EventArgs.
Just that I understand, wouldn't it be easier to create a procedure that is being called from either the ToolBar's click event or any other part of your program?
best regards
Sascha
That is something I did while trying to find the answer to my question. It's a matter of personal choice I suppose but I'd rather not go another function deep if/when the toolbar ButtonClick event can be used.