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.
LVL 1
bruncheyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
MirkwoodConnect With a Mentor Commented:
This code will do the trick.
I use send message to the toolbar window. The message is WM_COMMAND. The wParam is 100  + index of the button -1 the lParam is the window handle of the subclassed Comcontrol. The findwindow will take care of retrieving that hwnd.

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_COMMAND = &H111

Dim hwndOCX As Long
Dim hwndDll As Long

Private Sub Command1_Click()
    SendMessage hwndOCX, WM_COMMAND, 100 + 6 - 1, ByVal hwndDll
End Sub

Private Sub Form_Load()
    hwndOCX = Toolbar1.hwnd
    hwndDll = FindWindowEx(hwndOCX, 0, "ToolbarWindow32", vbNullString)
End Sub

Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)

End Sub

0
 
JayMerrittCommented:
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.
0
 
bruncheyAuthor Commented:
I know the obvious solutions, I was wondering if someone knew how to raise this event.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
wdeemCommented:
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

0
 
bruncheyAuthor Commented:
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.  
0
 
bruncheyAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.