How to Call Event Procs. w/No Param. from VB Form
ID: Q118645
|
The information in this article applies to:
-
Microsoft Visual Basic Standard and Professional Editions for Windows, versions 2.0, 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:
- Start a new project in Visual Basic. Form1 is created by default.
- Create a new form and add it to the project (its default name will be
Form2).
- On Form2, draw a command button and set its Visible property to "False"
and its Name property to "cmdEventFire".
- 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.
- 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).
- 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 :
Version :
Platform :
Issue type :