Trapping Events in Visual Basic Express Edition 2008

Keith_McCloy

Member
Joined
Dec 13, 2011
Messages
6
Programming Experience
10+
My reference for this is Francesco Balena's Visual Basic.NET from 2002, so it may be out of date.
I have created a simple class (MyNameClass) of two strings (first and last names) and an Event, FullName(ByVal fullName As String) that just contains a Constructor (New(astring, bstring)) and a Function (XFullName() to create and return the Full Name as the join of the two strings, as well as a RaiseEvent FullName(astring). Then I created a simple application that created an array called names() of type MyNameClass by placing values in the two fields for each entry in the array, and a button that created and displayed the full name.
First I tried inserting an AddHandler names(kurrent).FullName, AddressOf FullNameEvent in this Button1_Click sub and a Sub;-


Sub FullNameEvent(ByVal astring As String)
Dim bstring As String = Nothing
bstring = "Event Full Name used for " & astring
MsgBox(bstring, MsgBoxStyle.OkOnly,
"Event Trapping")
End Sub

But whilst the processing passed through the AddHandler commmand it never went into the FullNameEvent Sub. So, then I removed that AddHandler and tried adding a Sub to AddHandler p.FullName, AddressOf FullNameEvent for each member of the array of names(). Bu that does not work either. The program works; but the event trapping message does not happen.
I would appreciate any advice. :friendly_wink:
 
First up, FullName is a very bad name for an event. An event is supposed to be a notification that something happened. What exactly does the name FullName imply happened? It doesn't. Maybe something like FullNameChanged would be appropriate to indicate that a property named FullName had changed, but you have no such property.

A much better example would be to create a Person class with a GivenName property, a FamilyName property and, if you wanted, a FullName property that is ReadOnly. That FullName property would internally combine the values of the other two. You could possibly add another property to indicate whether FullName was formatted as 'GivenName FamilyName' or 'FamilyName, GivenName'. You would then add one or two or, less likely, three events. You could add GivenNameChanged and FamilyNameChanged events or you could add a FullNameChanged event. You might add all three but it would be rather pointless. Inside the appropriate properties you would then call the appropriate method to raise the appropriate event when that property value changed.

For examples and a full explanation, follow the Blog link in my signature and check out my post on Custom Events. From the sound of things, that will serve you better than the text you're reading now.
 
Hi!,

This is really good advice, but it has not helped me solve my problem. Having a problem in an application that I am building, I made a very simple application to look at the problem; and maybe I have been too quick and dirty in this simple application; but I just want to understand why I cannot trap events. Thanks. Keith
 
Either you're not raising the event properly or you're not handling the event properly. Both are very simple, as demonstrated below.
Public Class Form1

    'Because it is declared WithEvents, this variable can appear in a Handles clause.
    Private WithEvents thing1 As New Thing With {.Name = "Thing1"}

    'This variable will require AddHandler to attach an event handler.
    Private thing2 As New Thing With {.Name = "Thing2"}

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler thing2.DataChanged, AddressOf thing_DataChanged
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        thing1.Data = TextBox1.Text
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        thing2.Data = TextBox1.Text
    End Sub

    Private Sub thing_DataChanged(sender As Object, e As EventArgs) Handles thing1.DataChanged
        Dim something = DirectCast(sender, Thing)

        MessageBox.Show(String.Format("The Data property of {0} was changed to ""{1}"".",
                                      something.Name,
                                      something.Data))
    End Sub

End Class


Public Class Thing

    Private _data As String

    Public Property Name As String

    Public Property Data() As String
        Get
            Return _data
        End Get
        Set(ByVal value As String)
            If _data <> value Then
                _data = value
                OnDataChanged(EventArgs.Empty)
            End If
        End Set
    End Property

    Public Event DataChanged As EventHandler

    Protected Overridable Sub OnDataChanged(e As EventArgs)
        RaiseEvent DataChanged(Me, e)
    End Sub

End Class
 
Dear John,

Thanks for the help, both here and on your Blogg. It has been greatly appreciated. Now I have got them working in a simple example and think that I understand the process.

Keih
 
Back
Top