9 Replies - 354 Views - Last Post: 10 January 2018 - 10:05 AM Rate Topic: -----

#1 Poppa Mintin   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 13-September 08

Why does my Label not update ?

Posted 04 January 2018 - 08:29 AM

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
        Else
            UpDate(Form3.Label1, "Sorry... " & palName & "  didn't accept!")
        End If


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))
        Else
            Lab.Text = txt
            Lab.Refresh()
        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.

Poppa.

This post has been edited by Poppa Mintin: 04 January 2018 - 08:38 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Why does my Label not update ?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,764
  • Joined: 12-June 08

Re: Why does my Label not update ?

Posted 04 January 2018 - 08:32 AM

I am not following. The 'if palpc.connected' doesn't execute after you try/catch is over on line 3, or is something else not working right?
Was This Post Helpful? 0
  • +
  • -

#3 Poppa Mintin   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 13-September 08

Re: Why does my Label not update ?

Posted 04 January 2018 - 08:44 AM

View Postmodi123_1, on 04 January 2018 - 08:32 AM, said:

I am not following. The 'if palpc.connected' doesn't execute after you try/catch is over on line 3, or is something else not working right?

OK...

When the connection fails, the exception runs, so:

palPC.connected is False so the 'Else' code calls the Form3 Label1 text update...

And although I can watch the code complete correctly the text doesn't actually change.


Poppa.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,764
  • Joined: 12-June 08

Re: Why does my Label not update ?

Posted 04 January 2018 - 08:50 AM

Is 'Form3' not the form this is happening on?

If not did you create an actual instance of that Form3 object and show it? If not you need to.

dim foo = new Form3
foo.Label1 = "blah blah"
Was This Post Helpful? 0
  • +
  • -

#5 Poppa Mintin   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 13-September 08

Re: Why does my Label not update ?

Posted 04 January 2018 - 09:04 AM

View Postmodi123_1, on 04 January 2018 - 08:50 AM, said:

Is 'Form3' not the form this is happening on?

If not did you create an actual instance of that Form3 object and show it? If not you need to.

dim foo = new Form3
foo.Label1 = "blah blah"

You seem to be saying do this:

    Private Sub UpDate(ByVal Lab As Label, ByVal txt As String)
        Dim frm3 As Form = New Form3

        If Me.InvokeRequired Then
            Me.Invoke(Sub() UpDate(Lab, txt))
        Else
            frm3.Lab.Text = txt
            frm3.Lab.Refresh()
        End If
    End Sub


But Intellisence says ' Lab is not a member of Form '. This may be because I have Option Strict on by default.


Poppa.

PS. Oh, I miss-read that. Form3 does exist.


Pop.

This post has been edited by Poppa Mintin: 04 January 2018 - 09:09 AM

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,764
  • Joined: 12-June 08

Re: Why does my Label not update ?

Posted 04 January 2018 - 09:08 AM

What is '.lab'? Do you mean 'label'?
Was This Post Helpful? 0
  • +
  • -

#7 Poppa Mintin   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 13-September 08

Re: Why does my Label not update ?

Posted 04 January 2018 - 09:16 AM

View Postmodi123_1, on 04 January 2018 - 09:08 AM, said:

What is '.lab'? Do you mean 'label'?

Lab is a variable passed to the subroutine:

Private Sub UpDate(ByVal Lab As Label, ByVal txt As String)

I need to pass which Label on which Form by passing them as variables to the UpDate Sub.
In this case I use ' Form3.Label1 '.


Poppa.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,764
  • Joined: 12-June 08

Re: Why does my Label not update ?

Posted 04 January 2018 - 09:19 AM

Does your form have a global variable 'lab' or what?

It sounds like you think you take that parameter 'lab' and do.. something.. with it as if it was a property/variable of a completely different, and unrelated, object.
Was This Post Helpful? 0
  • +
  • -

#9 Poppa Mintin   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 13-September 08

Re: Why does my Label not update ?

Posted 04 January 2018 - 09:58 AM

View Postmodi123_1, on 04 January 2018 - 09:19 AM, said:

Does your form have a global variable 'lab' or what?

It sounds like you think you take that parameter 'lab' and do.. something.. with it as if it was a property/variable of a completely different, and unrelated, object.


I'm coding in vb.NET.

If I make a subroutine and add atributes then those variables become pointers to the objects assigned to them... In this case 'Lab' is a variable which represents a Label, and for the purpose of this particular pass to the subroutine it represents Form3.Label1.
When an Invoke is not required the text (passed to the subroutine as the String variable ' txt ') is set in the label directly, it's only when an invoke is required that my method is not working.


Poppa.
Was This Post Helpful? 0
  • +
  • -

#10 PeterH   User is offline

  • D.I.C Regular

Reputation: 58
  • View blog
  • Posts: 282
  • Joined: 03-September 09

Re: Why does my Label not update ?

Posted 10 January 2018 - 10:05 AM

I believe Mod123 is getting at how you created the form on which the label exists. Somewhere in your code before you show the relevant form, you should be creating an instance of it i.e. dim myform as new Form3 etc. etc. If you do not do this you are using the default form and will get into all sorts of problems.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1