Click here to Skip to main content
14,299,952 members
Rate this:
Please Sign up or sign in to vote.
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
AddHandler myComPort.DataReceived, AddressOf port_ATE
This attaches the event handler, so when copying it over I changed the name to
AddHandler AteComPort.DataReceived, AddressOf port_ATE
I have also changed the various names as below
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 !

 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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

 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
   
v2
Comments
glennPattonWork 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.
glennPattonWork 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
glennPattonWork 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.
Mike Meinz 19-Mar-14 13:19pm
   
It sounds like you coded the declaration of the SerialPort control yourself.

If you use the Toolbox and drag the SerialPort control onto your form, the proper declaration is created for you and a property sheet is shown to declare initialization properties. The Toolbox is found in Visual Studio under the View menu.
glennPattonWork 19-Mar-14 13:26pm
   
Like I said I'm really a C# guy for Windows which does seem to be a little easier don't really like VB...I also avoid using the Tool Box and roll my own due to bad memories of VB6/MSComm!
Mike Meinz 19-Mar-14 13:29pm
   
VB has come a long way since VB6. The Toolbox can make coding a lot easier.
glennPattonWork 19-Mar-14 13:30pm
   
We can but hope!

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100