Relatively, the use of the event is relatively simple. Before use, we must first define this event, which means notifying Visual Basic what event is called. An event may have its own parameters, for example, a COMMAND button has a Click event, which does not have a parameter. In addition, the text edit box has a KeyPress event, which handles related content through a value called "keyascii".
Define an event is to add similar to the following code in a class of universal declaration:
Public Event MyEventName (Possarguments As String, ETC As Variant)
Then call the RaiseEvent method in the code to inspire an event. Just like the code below:
RaiseEvent MyEventName ("Possargs", "ETC")
In order to better explain the process of adding and excitation events, we will give an example. First, define an event:
Add the following code to the General Declaration section of the CDOG class:
Public event awake ()
Add a SLEEP child process in the CDOG class:
Public SUB SLEP () DIM I as Long for i = 1 to 1000000 DOEvents: doevents: doevents exit raiseevent awakend sub
In the code, do some 10,000 copies of loops at the beginning, the computer is short-lived, and the SLEEP sub-process excites the AWake event.
But after the AWAKE event is generated, should we let the program respond accordingly? Of course, using the command button is the easiest, as long as the command button object is selected in the list of code windows.
But in that, we must need a control, and what you see is on the form. Here we use the corresponding code to use the corresponding code and is invisible.
Of course, use code to receive events, requiring additional operations:
Add the following code to the General Declaration section of the Form Code window:
DIM WITHEVENTS MYDOG AS CDOG
This code is different from the previous MyDog declaration, which has a keyword witHevents used to notify Visual Basic that the object can be charged any event, and the object must receive an event.
Delete all the code in the command button; add the following code in Command1:
Set mydog = new cdogmydog.name = "billy" mydog.barkmydog.sleep
This code simply sets myDog to a new instance of CDOG, set up the NAME, call the Bark, and finally run the SLEP sub-process.
Now add some code to the corresponding AWAKE event.
In the Form Code window, select "MyDog" from the object drop-down list;
Add the following code in "AWake" event in "MyDog":
Private sub mydog_awake () msgbox "Your Pooch Has Awoke!" End Sub
Ok, you can test it now.
Press F5 to run the program;
Click the Command button;
In this way, when the puppy Bark starts, start to fight, and finally wake up at the end. It's amazing!