Link to home
Start Free TrialLog in
Avatar of brunchey
brunchey

asked on

Raising a Toolbar Button Click event using code or API calls

I have an application where we are trying to localize all of the code to turn on and off buttons on a toolbar within the active form. We also would like to have a menu bar within the application, and have this remain in sync with the toolbar. This is pretty much no problem.  Then to communicate what button has been clicked on, we have a toolbar object in each form that is using the WithEvents keyword to listen to the button bar and act accordingly, this also works great.  The problem we are having is that when the user selects the same option from the menu bar we would like to have this action raise the appropriate toolbar event, the toolbar_click event.  The only problem is
we have no idea how to do this.  We have tried calling the toolbar_click sub passing in the appropriate button, but it did not work.  I am wondering if anyone knows an API call to use that will raise events for a specified control.
Avatar of JayMerritt
JayMerritt

Instead, move the code out of the toolbar_click event and put it into a public sub which can be called by the click event or the menu option.
Avatar of brunchey

ASKER

I know the obvious solutions, I was wondering if someone knew how to raise this event.
This code works with the standard VB5 Toolbar_Click event and no API calls. It passes a button from the toolbar (selected by Key, but you could also select by Index) to the click event so that one click subroutine handles everything.

Private Sub mnuPrint_Click() 'Occurs when Print is clicked on Menu
    Dim theButton As Button

    Set theButton = Toolbar.Buttons("Print")
    Call Toolbar_ButtonClick(theButton)
    Set theButton = Nothing
End Sub

Private Sub mnuSave_Click() 'Occurs when Save is clicked on Menu
    Dim theButton As Button

    Set theButton = Toolbar.Buttons("Save")
    Call Toolbar_ButtonClick(theButton)
    Set theButton = Nothing
End Sub

Private Sub Toolbar_ButtonClick(ByVal Button As ComctlLib.Button)
    Select Case Button.Key
        Case "Print"
            'print code goes here
        Case "Save"
            'save code goes here
    End Select
End Sub

Okay, apparently I have not specified my problem well enough.  I am using WITHEVENTS in all of my forms to capture the toolbar click events IN THE FORM.  I have tried calling the toolbar_click event myself through code, but it did not work because it did not acutally raise the toolbar_click event.  I need to raise this event and have it be broadcasted by the windows messaging manager.  
ASKER CERTIFIED SOLUTION
Avatar of Mirkwood
Mirkwood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I 'll say that you gave me a good answer Mirkwood.  I have not used it because we needed to distrubute the application, but for future use, it looks good.  THanks.