ईवेंट्स
17 मार्च, 9 pm - 21 मार्च, 10 am
साथी डेवलपर्स और विशेषज्ञों के साथ वास्तविक दुनिया के उपयोग के मामलों के आधार पर स्केलेबल एआई समाधान बनाने के लिए मीटअप श्रृंखला में शामिल हों।
अभी पंजीकरण करेंयह ब्राउज़र अब समर्थित नहीं है.
नवीनतम सुविधाओं, सुरक्षा अपडेट और तकनीकी सहायता का लाभ लेने के लिए Microsoft Edge में अपग्रेड करें.
This walkthrough demonstrates how to declare and raise events for a class named Widget
. After you complete the steps, you might want to read the companion topic, Walkthrough: Handling Events, which shows how to use events from Widget
objects to provide status information in an application.
Assume for the moment that you have a Widget
class. Your Widget
class has a method that can take a long time to execute, and you want your application to be able to put up some kind of completion indicator.
Of course, you could make the Widget
object show a percent-complete dialog box, but then you would be stuck with that dialog box in every project in which you used the Widget
class. A good principle of object design is to let the application that uses an object handle the user interface—unless the whole purpose of the object is to manage a form or dialog box.
The purpose of Widget
is to perform other tasks, so it is better to add a PercentDone
event and let the procedure that calls Widget
's methods handle that event and display status updates. The PercentDone
event can also provide a mechanism for canceling the task.
Open a new Visual Basic Windows Application project and create a form named Form1
.
Add two buttons and a label to Form1
.
Name the objects as shown in the following table.
Object | Property | Setting |
---|---|---|
Button1 |
Text |
Start Task |
Button2 |
Text |
Cancel |
Label |
(Name) , Text |
lblPercentDone, 0 |
On the Project menu, choose Add Class to add a class named Widget.vb
to the project.
Use the Event
keyword to declare an event in the Widget
class. Note that an event can have ByVal
and ByRef
arguments, as Widget
's PercentDone
event demonstrates:
Public Event PercentDone(ByVal Percent As Single,
ByRef Cancel As Boolean)
When the calling object receives a PercentDone
event, the Percent
argument contains the percentage of the task that is complete. The Cancel
argument can be set to True
to cancel the method that raised the event.
नोट
You can declare event arguments just as you do arguments of procedures, with the following exceptions: Events cannot have Optional
or ParamArray
arguments, and events do not have return values.
The PercentDone
event is raised by the LongTask
method of the Widget
class. LongTask
takes two arguments: the length of time the method pretends to be doing work, and the minimum time interval before LongTask
pauses to raise the PercentDone
event.
To simplify access to the Timer
property used by this class, add an Imports
statement to the top of the declarations section of your class module, above the Class Widget
statement.
Imports Microsoft.VisualBasic.DateAndTime
Add the following code to the Widget
class:
Public Sub LongTask(ByVal Duration As Single,
ByVal MinimumInterval As Single)
Dim Threshold As Single
Dim Start As Single
Dim blnCancel As Boolean
' The Timer property of the DateAndTime object returns the seconds
' and milliseconds that have passed since midnight.
Start = CSng(Timer)
Threshold = MinimumInterval
Do While CSng(Timer) < (Start + Duration)
' In a real application, some unit of work would
' be done here each time through the loop.
If CSng(Timer) > (Start + Threshold) Then
RaiseEvent PercentDone(
Threshold / Duration, blnCancel)
' Check to see if the operation was canceled.
If blnCancel Then Exit Sub
Threshold = Threshold + MinimumInterval
End If
Loop
End Sub
When your application calls the LongTask
method, the Widget
class raises the PercentDone
event every MinimumInterval
seconds. When the event returns, LongTask
checks to see if the Cancel
argument was set to True
.
A few disclaimers are necessary here. For simplicity, the LongTask
procedure assumes you know in advance how long the task will take. This is almost never the case. Dividing tasks into chunks of even size can be difficult, and often what matters most to users is simply the amount of time that passes before they get an indication that something is happening.
You may have spotted another flaw in this sample. The Timer
property returns the number of seconds that have passed since midnight; therefore, the application gets stuck if it is started just before midnight. A more careful approach to measuring time would take boundary conditions such as this into consideration, or avoid them altogether, using properties such as Now
.
Now that the Widget
class can raise events, you can move to the next walkthrough. Walkthrough: Handling Events demonstrates how to use WithEvents
to associate an event handler with the PercentDone
event.
.NET प्रतिक्रिया
.NET एक ओपन सोर्स प्रोजेक्ट है. प्रतिक्रिया प्रदान करने के लिए लिंक का चयन करें:
ईवेंट्स
17 मार्च, 9 pm - 21 मार्च, 10 am
साथी डेवलपर्स और विशेषज्ञों के साथ वास्तविक दुनिया के उपयोग के मामलों के आधार पर स्केलेबल एआई समाधान बनाने के लिए मीटअप श्रृंखला में शामिल हों।
अभी पंजीकरण करेंप्रशिक्षण
मॉड्यूल
Maak een gebruikersinterface die gebruikmaakt van gegevensbinding in .NET MAUI. - Training
Maak een gebruikersinterface met gegevensbinding. Uw gebruikersinterface wordt automatisch bijgewerkt op basis van de meest recente gegevens, terwijl de gegevens worden bijgewerkt als reactie op wijzigingen in de gebruikersinterface.
दस्तावेज़ीकरण
Meer informatie over: Gebeurtenissen (Visual Basic)
Gebeurtenissen verwerken - Visual Basic
Meer informatie over: Walkthrough: Gebeurtenissen verwerken (Visual Basic)
Procedure: Aangepaste gebeurtenissen declareren om blokkeren te voorkomen - Visual Basic
Meer informatie over: Aangepaste gebeurtenissen declareren om blokkeren te voorkomen (Visual Basic)