Raising a Toolbar Button Click event using code or API calls

Posted on 1998-08-25
Last Modified: 2008-02-01
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.
Question by:brunchey
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1430950
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.

Author Comment

ID: 1430951
I know the obvious solutions, I was wondering if someone knew how to raise this event.

Expert Comment

ID: 1430952
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1430953
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.  
LVL 13

Accepted Solution

Mirkwood earned 200 total points
ID: 1430954
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


Author Comment

ID: 1430955
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.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question