Search code examples
vb6

call a toolbar button click from another form


In VB6 I need to know how to call a button click event on anther form. The another form part is easy but how to pass the click event the proper method to "click" the right button on the toolbar is the real issue.

Here is the vent on the main form - i need to call the click event case "Copyfrom".

MainForm

Public Sub tbrMain_ButtonClick(ByVal Button As MSComctlLib.Button)

Select Case Button.Index
  Case ToolBarItem.tbPrint

    '(some code)

  Case ToolBarItem.tbSave

    '(some code)

  Case ToolBarItem.tbCopyFrom

    '(some code)

   Case ToolBarItem.tbNEW

    '(etc)

I tried

Mainform.tbrMain_ButtonClick() 

and even tried passing the index number and key - no dice.


Solution

  • The event handler is expecting to receive a reference to an actual toolbar button, so you have to pass the toolbar button itself, not it's Caption or Key, e.g.:

        Form1.tbrMain_ButtonClick Form1.tbrMain.Buttons(1)
    

    Or, using the Call statement:

        Call Form1.tbrMain_ButtonClick(Form1.tbrMain.Buttons(1))
    

    If you set the Key properties on your toolbar buttons, you can use the Key property of the desired button in place of the (1):

        Form1.tbrMain_ButtonClick Form1.tbrMain.Buttons("PrintButton")