Results 1 to 11 of 11

Thread: How do I use Event Handlers

Threaded View

  1. #1

    Thread Starter
    Lively Member Brian Henry's Avatar
    Join Date
    Oct 2005
    Posts
    94

    How do I use Event Handlers

    Hi all I'm am trying to do sum custom events in a Class. My class listens for a UDP Broadcast, I want to Raise and Event when a message is received to display the message from a windows form. I am having no luck. Can sum one help.

    Here is my code.

    Code:
    Public Class UDP_Broadcast
        Public Event NewMessage(ByVal Message As String)
        Public Port As Integer = 2456
    
        Public Sub Receiver_Load()
            Dim t As New Threading.Thread(AddressOf listen)
            t.IsBackground = True
            t.Start()
        End Sub
    
        Public Sub Receiver_Closing()
            If udp IsNot Nothing Then udp.Close()
        End Sub
    
        Private udp As Net.Sockets.UdpClient
        Private Sub listen()
            Try
                udp = New Net.Sockets.UdpClient(Port)
                udp.EnableBroadcast = True
                Dim ep As New Net.IPEndPoint(Net.IPAddress.Broadcast, Port)
                Do
                    Dim b() As Byte = udp.Receive(ep)
                    'Me.Invoke(safeAddText, System.Text.Encoding.UTF32.GetString(b))
                    safeAddText(System.Text.Encoding.UTF32.GetString(b))
                Loop
            Catch ex As Exception
                If udp IsNot Nothing Then udp.Close()
            End Try
        End Sub
    
        Private Delegate Sub delAddText(ByVal text As String)
        Private safeAddText As New delAddText(AddressOf AddText)
        Private Sub AddText(ByVal text As String)
            RaiseEvent NewMessage(text)
            'MsgBox(text)
        End Sub
    
    End Class
    Form Code
    Code:
     Private WithEvents Mess As UDP_Broadcast
        Private Sub UDP_Broadcast1(New_UDP_Mess As String) Handles Mess.NewMessage
            MsgBox(New_UDP_Mess)
        End Sub
    Last edited by Brian Henry; Feb 20th, 2012 at 03:50 PM.
    Brian Henry
    Visual Studio 2001 to 2019
    Java/Android
    ISaGRAF

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width