1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
Imports System
Imports System.Threading
Public Class Form1
Public Class GenerateTextArgs ' déclaration de la classe pour la création de l'événement
Inherits EventArgs
Private myEventText As String = Nothing 'declaration d'un membre string attribut de la classe
Public Sub New(ByVal theEventText As String) 'constructeur surchargé de la classe
If theEventText Is Nothing Then
Throw New NullReferenceException()
End If
myEventText = theEventText
End Sub
Public ReadOnly Property EventText As String ' accesseur
Get
Return Me.myEventText
End Get
End Property
End Class
Public Class GeneratText
Public Delegate Sub TextGeneratedEventHandler(ByVal sender As Object, ByVal e As GenerateTextArgs) 'déclaration du delegate pour générer l'événement
Public Event OnTextChanged As TextGeneratedEventHandler 'déclaration d'un objet event du type du delegate déclaré plus haut
Public Sub New()
End Sub
Public Sub Start(theNumber As Integer) 'declaration de la method
Dim i As Integer = 10
While i < theNumber
Dim e As GenerateTextArgs = New GenerateTextArgs("Compteur = " & i.ToString())
If Not (e Is Nothing) Then
MsgBox("compteur:" & i.ToString)
RaiseEvent OnTextChanged(Me, e)
End If
Thread.Sleep(1000)
i = i + 1
End While
End Sub
End Class
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim obj2 As New GeneratText
obj2.Start(15)
End Sub |
Partager