我有一个 Vb.net 应用程序。其中包含一个列表框。
我想从另一个类使用事件在这个列表框中添加消息。
基本上我正在尝试创建状态记录器,所以我可以记录步骤并显示到列表框中。
创建一个定义了事件的新类
Public Class EventTest
Public Event StatusChanged(ByVal status As String)
Private _status As String = ""
Public Property Status As String
Get
Return _status
End Get
Set(value As String)
_status = value
'raise the status changed event when the status property is changed
RaiseEvent StatusChanged(value)
End Set
End Property
End Class
现在,在您的表单中创建此类的一个实例(注意withevents
关键字):
Dim WithEvents Foo As New EventTest
'Handle the status change event
Private Sub Foo_StatusChanged(status As String) Handles Foo.StatusChanged
lstStatus.items.add(status)
End Sub
现在更改状态并看到它出现在列表框中:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Foo.Status = "Initialised"
End Sub