EVENT statement

xiaoxiao2021-03-06  143

EVENT statement

Define user-defined events.

grammar

[Public] Event procedureName [(arglist)]

The EVENT statement contains the following sections:

Partially describes public options. Specifies that the Event is visible throughout the project. By default, the Events type is public. It should be noted that the event can only be generated in the declared module. ProcedureName must be required. The name of the event; following the standard variable naming convention.

The syntax and grammar of the arglist parameter is as follows:

[Byval | Byref] Varname [()] [As Type]

Partial description Byval is optional. This parameter is transmitted by value. Byref is optional. Indicates that the parameter is passed by address. BYREF is the default setting of Visual Basic. Varname must be required. Represents the name of the parameter variable to be passed to the process; follow the standard variable naming convention. Type is optional. Refers to the data type of the parameter passing to the process; can be byte, Boole, Integer, long, currency, single, double, decimal (currently not supported), Date, String (support only), Object, Variant, user definition Type or object type.

Description

After the event is declared, you can use the RaiseEvent statement to generate this event. If an EVENT declaration occurs in a standard module, a syntax error will occur. Unable to declare events with return values. In the following code segment, a typical events for declaration events and events are given:

'Declare an event in the module level of the class module

Event logOncompleted (username as string)

Sub raiseevent logOncompleted ("Antoinejan") End Sub

Note that you can declare the parameters of the event like the parameters of the declaration process, but there is a difference: the event does not have a naming parameter, optional parameter, or Paramarray parameter. The event has no return value.

EVENT statement example The following example is when using an event in a 100-meter run demo. The code illustrates all methods, attributes, and statements related to events, including EVENT statements.

The class that generates an event is an event source, and the class that implements the event is an event absorption. An event source can have a plurality of leaks for events they produce. Events can be triggered by each selected instance of an object.

This example uses a form (Form1), which has a button (Command1), a label (label1), and two text boxes (Text1, and Text2). After clicking the button, the first text box displays "from now", the clock in the second text box starts. When after 9.84 seconds (100 meters record), the first text box displays "Until Now", the second display "9.84".

The Form1 code is used to specify the initial state and termination state of the form, and also contain the code to be executed after the event is generated.

Option expedition

Private Withevents MText As Timerstate

Private sub fascist1_click () text1.text = "from now" text1.refresh text2.text = "0" text2.refreshcall mtext.timertask (9.84) End Sub

Private Sub Form_Load () Command1.Caption = "Click to Start Timer" Text1.Text = "" Text2.Text = "" Label1.Caption = "The fastest 100 meter run took this long:" Set mText = New TimerState End SubPrivate Sub MTEXT_CHANGETEXT () text1.text = "until now" text2.text = "9.84" End Sub

Private sub mtext_updatetime (byval dbljump as double) text2.text = Str (Format (DBLjump, "0")) DOEventsend Sub

Below is the code of the TiMerstate class module. The EVENT statement declaration is initialized after the event generation.

Option Explicitpublic Event UpdateTime (Byval DBLJUMP As Double) Public Event ChangeText ()

Public Sub TimerTask (ByVal Duration As Double) Dim dblStart As Double Dim dblSecond As Double Dim dblSoFar As Double dblStart = Timer dblSoFar = dblStart Do While Timer = 1 Then dblSoFar = dblSoFar 1 RaiseEvent UpdateTime ( Timer - dblstart) Endiff RaiseEvent ChangeText End Sub

Events to handle objects

The object of the incident is called an event source. In order to deal with events caused by the event source, the variables of the object class can be declared with the WitHevents keyword.

This topic continues to discuss examples of Widget objects starting in the "declaration and incident". In order to deal with the Widget's PercentDone event, the following code is placed to the declaration of Form1:

Option ExplicitPrivate WitHevents Mwidget As WidgetPrivate Mblncancel as Boolean

WitHevents Keyword Specifies: Variable MWIDGET will be used to handle an object. You can specify the object type by providing a class name, which is a class that creates this object.

The variable MWidget is declared in the "Form1" declaration section, because the WitHevents variable must be a modular variable. This is correct, regardless of the type of module places them.

Variable MBLNCANCEL will be used to cancel the longtask method.

Some restrictions on WitHevents variables When using the WitHevents variable, you should pay attention to these restrictions:

The WitHevents variable cannot be a derived object variable. That is, it is not possible to declare it as AS Object- The class name must be specified when declaring the variable.

You cannot declare the WitHevents variable as AS New. The event source object must be explicitly created and it assigns it to the WitHevents variable.

The WitHevents variable cannot be declared in the standard module. You can only declare in modules of class modules, form modules, and other definition classes. Unable to create an array of WitHevents variables. The code to write the handling event once declares the WitHevents variable, the variable name appears on the drop down menu left on the left side of the module "Code" window. When the MWIDGET is selected, the event of the Widget class will appear on the right drop-down menu, as shown in Figure 9.9 below.

Figure 9.9 Events associated with WitHevents variables

Select an event, will display the corresponding event process, with mWidget_ a prefix. All event processes associated with the WitHevents variable will be prefixed in this variable. Add the following code to the MWIDGET_PERCENTDONE event.

Private Sub Mwidget_PercentDone (Byval Percent as _Single, Cancel As Boolean) LBLPERCENTDONE.CAPTION = CINT (100 * percent) & "%" doevents if mblncancel the can Cancel = TrueEnd SUB

Whenever, when the Percentdone event is triggered, the event process displays the percentage of the completion in the Label control. The DOEvents statement allows you to rearrange the tag, and also give the user a chance to click the "Cancel" button. Add the following code to the CLICK event of the title "Cancel" button.

Private submmand2_click () mblncancel = truend sub

When longtask is running, if you click the "Cancel" button, once the DOEvents statement allows the process to process the event, the Command2_Click event will be executed. The module-level variable MBLNCANCEL is set to True, and the MWidget_PerCentDone event will test it and set the ByRef Cancel parameter to True.

Connecting the WitHevents variable and object Form1 has set the processing of the Widget object event. The remaining work is to find a Widget in a place.

When designing, when declaring the WitHevents variable, there is no object to be associated with it. The WitHevents variable is the same as any other object variable. You must create an object and assign the reference to this object to this WitHevents variable. Add the following code to the Form_Load event process to create a Widget.

PRIVATE SUB FORM_LOAD () SET MWIDGET = New Widgetend Sub

When the above code is performed, Visual Basic will create a widget and connect its event with the event process associated with the MWIDGET. Since then, once the widget generates the PercentDone event, the MWIDGET_PERCENTDONE event process will be executed.

In order to call the longtask method, the following code is added to the Click event headed as the "Startup Task" button.

'"Start Task" button. Private sub fascist1_click () mblncancel = false lblpercentdone.caption = "0%" LBLPERCENTDONE.REFRESH

Call MWidget.longTask (14.4, 0.66)

IF not mblncancel damtion = 100END SUB

When calling the longtask method, the display tag that completes the percentage must be initialized, and the module-level Boolean tag (which is a cancellation method) is set to false. Use 14.4 second task delay calling longtask. Events a PercentDone event every two seconds. The MWIDGET_PERCENTDONE event process will be executed each time the event is triggered.

After LongTask, you have to test MBLNCANCEL to see if longtask is ended normally, or whether it is stopped because MBLNCANCEL is set to TRUE. Only the percentage of completion will be updated only in the previous case.

Run the program Press the F5 key to make the project in operation mode. Click the "Start Task" button. Each time the percentdone event is triggered, the tag with completion percentage will be updated. Click the "Cancel" button to stop the task. Note: When you click the "Cancel" button, the appearance of the button will not change immediately. Click events are triggered until the DoEvents statement allows event processing.

It may be found that it is beneficial to run the program with the F8 key - doing the code each time. You can clearly see how to do longTask, and then you can re-enter Form1 when you trigger a PercentDone event.

What happens if you call the longtask method again if you are running back to Form1? Confusion, chaos, and final (this happens when an event occurs), stack overflows.

Handling Different Widget Events assigning a reference to the new Widget to the MWIDGET variable, allowing the MWIDGET variable to handle events for different Widget objects. In fact, each time you click the button, you can make the code in Command1 to do this by adding the following two lines:

Set mwidget = new widget '<- New Row. Call MWidget.longTask (14.4, 0.66) Set mwidget = Nothing '<- New Row.

When the button is pressed, the above code will create a new Widget. Once the longtask method ends, the reference to the Widget will be released by setting the MWidget to Nothing, and this widget is also revoked.

The WitHevents variable can only contain an object reference each time, so if you assign different Widget objects to the MWIDGET, the previous Widget object will not be processed. If the MWIDGET is the only object variable included with the old Widget reference, the object will be undo.

Note how much WitHevents variables are needed, but you can declare how much, but you do not support the WitHevents variable array.

Termination of the WITHEVENTS variable event is to have a Widget object that is assigned to the variable MWidget. Whenever, when the Widget generates an event, the event procedure associated with the MWIDGET will be called. In order to terminate the event processing, the MWidget can be set to Nothing, as shown in the following code block.

'Termination of the MWidget event. SET MWIDGET = Nothing

When the WitHevents variable is set to Noth, Visual Basic will remove the object's event and the connection to the event procedure associated with the variable.

The key WitHevents variable contains an object reference as any other object variable. The role of this object reference is to maintain the object is active. When all references to the object are set to Nothing to undo it, don't forget to declare the variable of WitHevents. Details The event procedure associated with the WitHevents variable looks like the event process of the control on the form. "Comparison of Control Events on WitHevents and Forms" discussed similarity and differences between them.

转载请注明原文地址:https://www.9cbs.com/read-127551.html

New Post(0)