How to Call Event Procs. w/No Param. from VB Form

Last reviewed: June 21, 1995
Article ID: Q118645
The information in this article applies to:
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0
  • Microsoft Visual Basic Programming System for Windows, version 1.0

SUMMARY

Normally, it is not possible to call event procedures of one form from another. However, there is a way to indirectly call event procedures that do not have any parameters passed to them from another form.

MORE INFORMATION

Event procedures are called internally by Windows in response to user events, like a mouse click or a keypress, associated with any control or object. These event procedures are not visible outside of the form to which they belong. They are private procedures and hence cannot be called directly from another form. In some cases, Windows passes a parameter to these procedures when it calls them. For example, the MouseDown event is passed the following four parameters:

   Sub Form_MouseDown (Button As Integer, Shift As Integer, X As Single,
   Y As Single)

Sometimes, you may want to call one of these event procedures from the application code itself. However this possible only if no parameters have to be passed, because these are totally dependent on the state of the system as Windows fires these events.

Step-by-Step Example

Here is an example that calls the Form_Click() event of Form2 from Form1:

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Create a new form and add it to the project (its default name will be Form2).

  3. On Form2, draw a command button and set its Visible property to "False" and its Name property to "cmdEventFire".

  4. Add the following code in the cmdEventFire_Click event:

         Sub cmdEventFire_Click ()
           Call Form_Click
         End Sub
    

    NOTE: This code could be a call to any Form event or procedure that does not require parameters.

  5. To fire Form2's Form_Click event, set the value of the cmdEventFire command button to "True". Setting the Value property of a Command Button causes the button to click and invokes the Command1_Click event. Add the following code to the Form_DblClick event on Form1 (You could also add this code in any other sub or function in any module in your application):

         Sub Form_DblClick ()
           Form2.cmdEventFire = True
         End Sub
    

    As a result, cmdEventFire's click event is called, which in turn calls l the event procedure of our choice on Form2 (in this case, Form_Click).

  6. Add the following code to the Form_Click event on Form2. This code will be indirectly called when you click Form1:

         Sub Form_Click ()
           MsgBox "Form2_Click was called without clicking on Form2."
         End Sub
    


Additional reference words: 2.00 3.00 1.00
KBCategory: kbprg kbcode
KBSubCategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.