Results 1 to 11 of 11

Thread: calling Toolbar button click event through code

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2002
    Posts
    10

    calling Toolbar button click event through code

    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.

  2. #2
    Junior Member husain's Avatar
    Join Date
    Jul 2002
    Location
    United Arab Emirates
    Posts
    31
    Assuming that your toolbar is called 'ToolBar':
    VB Code:
    1. Private Sub ToolBar_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar.ButtonClick
    2.     Select Case ToolBar.Buttons.IndexOf(e.Button)
    3.         Case 0    ' The first button
    4.         MsgBox("You pressed the new button")
    5.  
    6.         Case 1    ' The second button
    7.         MsgBox("You pressed the save button")
    8.     End Select
    9. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2002
    Posts
    10
    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.

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You do it the same as in VB6 only you have to pass the proper parameters along too.

    VB Code:
    1. Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick
    2.         'handle click event
    3.         MsgBox(String.Concat("You pushed button # ", ToolBar1.Buttons.IndexOf(e.Button)))
    4.     End Sub
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         'FIRE click event
    8.         ToolBar1_ButtonClick(ToolBar1, New ToolBarButtonClickEventArgs(ToolBarButton1))
    9.     End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2002
    Posts
    10
    Excellent, thank you Edneeis I'll give it a shot.

    It was the parameter format I couldn't figure out.

    O.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Alright its:
    SubThatHandlesEvent(ToolbarControl, New ToolBarButtonClickEventArgs(ToolbarButtonObject)

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2002
    Posts
    10
    Again, thank you very much. Just tested your advice and it worked perfectly. Thanks for the help.

    O.

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2002
    Posts
    10
    Just out or curiosity, where did you find information on passing the New ToolBarButtonClickEventArgs(buttonobject)?

    O.

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  10. #10
    New Member
    Join Date
    Sep 2002
    Location
    Vienna
    Posts
    13
    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

  11. #11

    Thread Starter
    New Member
    Join Date
    May 2002
    Posts
    10
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width