Click here to Skip to main content
15,886,077 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi All,

My latest question from the wonderful hacking job I am doing is 'Am I attaching the event handler right?' I am trying to get some code working, I have written bits and pieces and would like to get it all in one program. In the test bit of code I have
VB
AddHandler myComPort.DataReceived, AddressOf port_ATE
This attaches the event handler, so when copying it over I changed the name to
VB
AddHandler AteComPort.DataReceived, AddressOf port_ATE
I have also changed the various names as below
VB
Private Sub port_ATE(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)

    'ATE Event Handler
    TmrNoDataAtPortATE.Enabled = False
    InputDataATE = AteComPort.ReadExisting
    Reply_Status_ATE = REPLY_ATE.YES_REPLY

    If InputDataATE <> [String].Empty Then
        Me.BeginInvoke(New SetTextCallbackATE(AddressOf SetTextATE), New Object() {InputDataATE})
        '    MsgBox("here!")
    Else
        MsgBox("null")
    End If
    TmrNoDataAtPortATE.Enabled = False
    If (Reply_Status_ATE = REPLY_ATE.TIMEOUT_REPLY) Then
        Data_Back_ATE = "TIMEOUT"
    ElseIf (Reply_Status_ATE = REPLY_ATE.YES_REPLY) Then
        TmrNoDataAtPortATE.Enabled = False

    End If



End Sub


working version !

VB
 Private Sub port_ATE(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)

    NoDataAtPort.Enabled = False
    InputData = myComPort.ReadExisting
    Reply_Status = REPLY.YES_REPLY

    If InputData <> [String].Empty Then
        Me.BeginInvoke(New SetTextCallback(AddressOf SetText), New Object() {InputData})
        '    MsgBox("here!")
    Else
        MsgBox("null")
    End If
    TmrNoDataAtPort.Enabled = False
    If (Reply_Status = REPLY.TIMEOUT_REPLY) Then
        Data_Back = "TIMEOUT"
    ElseIf (Reply_Status = REPLY.YES_REPLY) Then
        NoDataAtPort.Enabled = False

    End If

End Sub

I think they are the same! can anyone spot a difference! Please help!!
Posted
Updated 19-Mar-14 6:54am
v2

1 solution

In VB.NET, you can add a Handles clause to a procedure and it will be recognized as the event handler.

VB.NET
 Private Sub port_DataReceived_1(ByVal sender As Object, _
      ByVal e As SerialDataReceivedEventArgs) Handles AteComPort.DataReceived
    NoDataAtPort.Enabled = False
    InputData = myComPort.ReadExisting
    Reply_Status = REPLY.YES_REPLY

    If InputData <> [String].Empty Then
        Me.BeginInvoke(New SetTextCallback(AddressOf SetText), New Object() {InputData})
        '    MsgBox("here!")
    Else
        MsgBox("null")
    End If
    TmrNoDataAtPort.Enabled = False
    If (Reply_Status = REPLY.TIMEOUT_REPLY) Then
        Data_Back = "TIMEOUT"
    ElseIf (Reply_Status = REPLY.YES_REPLY) Then
        NoDataAtPort.Enabled = False

    End If

End Sub
 
Share this answer
 
v2
Comments
glennPattonWork3 19-Mar-14 12:59pm    
Interesting...Giving it a go now...
Mike Meinz 19-Mar-14 13:04pm    
In Visual Studio "code" view, if you select a control in the left combobox just below the tab and then select the event in the right combobox, a procedure stub will be generated for you with the Handles clause already coded.
glennPattonWork3 19-Mar-14 13:04pm    
just tried that: get the blue line and Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
Mike Meinz 19-Mar-14 13:07pm    
Modify the declaration of the AteComPort to have WithEvents after the Dim word and before the AteComPort word.

Dim WithEvents AteComPort As New SerialPort

Note: Dim word could be Friend, Private, Public, Protected or Dim
glennPattonWork3 19-Mar-14 13:17pm    
Thanks, first time I have come across WithEvents (mind you this is the first serious hack at VB.Net I have had!) on a separate note it has got rid of the blue wiggle but still isn't working! I didn't want to sleep tonight anyway, thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900