Solved

Raising a Toolbar Button Click event using code or API calls

Posted on 1998-08-25
6
647 Views
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.
0
Comment
Question by:brunchey
6 Comments
 
LVL 1

Expert Comment

by:JayMerritt
Comment Utility
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
 
LVL 1

Author Comment

by:brunchey
Comment Utility
I know the obvious solutions, I was wondering if someone knew how to raise this event.
0
 
LVL 1

Expert Comment

by:wdeem
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:brunchey
Comment Utility
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
 
LVL 13

Accepted Solution

by:
Mirkwood earned 200 total points
Comment Utility
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
 
LVL 1

Author Comment

by:brunchey
Comment Utility
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now