Microsoft KB Archive/118645

From BetaArchive Wiki
Knowledge Base


Article ID: 118645

Article Last Modified on 12/9/2003



APPLIES TO

  • Microsoft Visual Basic 2.0 Standard Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 2.0 Professional Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 1.0 Standard Edition



This article was previously published under Q118645

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 query words: 2.00 3.00

Keywords: KB118645