Why does my Label not update ? RRS feed

  • Question

  • Hi,

    This is the relevant part of a Public Sub on Form1:

     Catch ex As Exception
                MsgBox("Client_Accept :" & vbCrLf & ex.ToString)
            End Try
            If palPC.Connected Then
                boss = True
                UpDate(Form3.Label1, "Sorry... " & palName & "  didn't accept!")
            End If[/Code]

    When I run the app. and the connection fails, 'palPC' dosn't connect, the Exception exectutes.
    When I click the OK on the MessageBox I expect the code to proceed to:

    'If palPC.Connected Then', and since palPC didn't connect I expect Form3 Label1 to up-date, but it doesn't.

    Here is the update code, which is also in Form1:
        Public Sub UpDate(ByVal Lab As Label, ByVal txt As String)
            If Me.InvokeRequired Then
                Me.Invoke(Sub() UpDate(Lab, txt))
                Lab.Text = txt
            End If
        End Sub
    Using a breakPoint I can see that:

    Lab.Name = "Label1"
    txt = "Sorry... MSI didn't accept!"

    It's that 'Lab.Name = "Label1"' that I don't understand, I expected to see "Form3.Label1".
    There are three forms in this test project, and running a test on this part of the app. Form3's Label1 doesn't update, but neither does the Label1 on Form1 which may've shed a bit of light on the matter had it done.

    I don't believe the fault is in the first bit of code in this post because when palPC does connect, everything else works correctly.
    I only discovered this problem when I deliberately tried to connect to a computer that's not listening for the request. (To check that the code works).
    Also, the up-date subroutine works everywhere else in the app. here for example is a line from a different subroutine, also in Form1 which does work:
    UpDate(Form3.Label1, "I will be the Red player." & vbCrLf & vbCrLf & "Inviting  " & palName & "  to play.")

    I see that in this case 'Lab.Name' is also just "Form1". Ah bother... but the invoke wasn't required, but I think that shows that the rest of the subroutine works as it ought. 
    Single stepping through the code to: ' txt = "Sorry... MSI didn't accept!" ' 
    At this point the invoke is required and I can watch the instructions being completed, yet when they are done and Form3 is 'Shown' again, Label1 is just as it was previously.

    Thursday, January 4, 2018 6:49 PM