By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,572 Members | 1,516 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,572 IT Pros & Developers. It's quick & easy.

Problem changing label text from different form

100+
P: 116
Hi

I have a form, say Form1, that contains Label1 and a subroutine doText() that loads text into Label1.

Expand|Select|Wrap|Line Numbers
  1. Public Sub doText()
  2.     Label1.Text = "Hello"
  3. End Sub
  4.  
Form1 opens Form2 via Form2.Showdialog().

Expand|Select|Wrap|Line Numbers
  1. Public Sub loadForm()
  2.     Form2.ShowDialog()
  3. End Sub
  4.  
A subroutine on Form2 triggers the doText() on Form1 to reload the text of Label1.

Expand|Select|Wrap|Line Numbers
  1. Public Sub changeText()
  2.     Form1.doText()
  3. End Sub
  4.  
The subroutine fires but Label1.Text does not change as for some reason it can't locate Label1.Text value. Please be advised that there is code that determines the new text prior to changing Label1 but there's simply no need for it here as that's not the issue.

I assume there's a way to open Form2 and target Label1 properly and I'm obviously not doing that.

Any suggestions as to the proper format of the code to get this to work?

Thanks
Feb 8 '13 #1
Share this Question
Share on Google+
10 Replies


100+
P: 116
For clarity....

If I run MsgBox("Old: " & Label1.Text) from Form1 it's fine. If I trigger doText() from Form2, the same line outputs "Old: ".

Something is not targeting the label correctly.

Thanks
Feb 8 '13 #2

IronRazer
P: 83
Hello robertybob,
I have checked your example code and it changed the label text and showed the text in the messagebox fine. There must be a problem somewhere else that is causing the problem. You will have to post the code you are actually using in the subs for us to be able to see what is wrong.
Feb 9 '13 #3

P: 31
HI robertybob,

if you have example please share with us because i have also face this problem.
Feb 9 '13 #4

100+
P: 116
Thanks for the replies - I tried this myself with a basic version this morning and it also seemed ok so am trying to sort out an extended version of the code to see if anyone can spot a conflict somewhere
Feb 9 '13 #5

100+
P: 116
Here is a snippet of code

Expand|Select|Wrap|Line Numbers
  1. 'FORM1
  2. Option Explicit On
  3.  
  4. Public Sub doText()
  5.         MsgBox("Old: " & Label1.Text) ' this fails to find Label1.Text but only when called from Form2
  6. ' remaining sub code irrelevant as this line fails
  7. End Sub
  8.  
  9. ' FORM2
  10.     Private Sub doStuff()
  11.         If TextBox1.Text <> "" Then
  12.                 TextBox1.Text = ""
  13.                 Label2.Text = "Success"
  14.                 Label2.ForeColor = Color.Lime
  15.                 Button1.Enabled = True
  16.                 Button1.Focus()
  17.                 Form1.doText()
  18.         End If
  19.     End Sub
  20.  
The Label1 text is blank initially and is populated by running the full doText sub on load of Form1.

If I run the sub from Form2, the first time the MsgBox does NOT find Label1.Text value but if I immediately run it again it DOES find the old value but it still DOESN'T update it when asked to do so via Label1.Text = "Hello".
Feb 9 '13 #6

IronRazer
P: 83
Hi robertybob,
I have checked this example and it is also working for me as long as label1.text has been set before the messagebox is shown. When is label1.text being set on form1. Are you sure it is being set before the doText sub is called or before the messagebox is being shown? That sounds like it could be the problem to me if text is shown the second time.
Feb 9 '13 #7

100+
P: 116
Hi IronRazor,

The onload for Form1 definitely populates the text for Label1 so it has a value before Form2 is opened (at least a visible value although I feel its actual value is still blank). The fact is that even if it finds the value for Label1.Text and shows it in the MsgBox, it still doesn't then replace the Label1.Text with the new text in the next line. Doesn't seem feasible but it's true.

Always seems to take the old value of the label but never replaces it - it's like the swapping of the text initially invalidates the label from any form other than the one containing it.
Feb 10 '13 #8

IronRazer
P: 83
I`m back,
Maybe i am just not understanding what your trying to do or how the code is set up in your program. I am guessing your code is to long or you just don`t want everyone to see it so, this is the code i have been testing. Can you make this code so it is set up to reproduce the problem and re-post the code so i can see it.

Form1.vb
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         Label1.Text = "Old Text"
  5.     End Sub
  6.  
  7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  8.         Form2.ShowDialog()
  9.     End Sub
  10.  
  11.     Public Sub doText()
  12.         MessageBox.Show("Old: " & Label1.Text)
  13.  
  14.         'I just used this to change the text so i
  15.         'could do it 2 times and see text change
  16.         'each time for testing.
  17.         If Label1.Text = "Old Text" Then
  18.             Label1.Text = "New Text"
  19.         Else
  20.             Label1.Text = "New Text 2"
  21.         End If
  22.     End Sub
  23. End Class
  24.  
Form2.vb
Expand|Select|Wrap|Line Numbers
  1. Public Class Form2
  2.  
  3.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  4.         doStuff()
  5.     End Sub
  6.  
  7.     Private Sub doStuff()
  8.         If TextBox1.Text <> "" Then
  9.             'I used dummy controls on form 2 for
  10.             'these just to reproduce the effect.
  11.             TextBox1.Text = ""
  12.             Label2.Text = "Success"
  13.             Label2.ForeColor = Color.Lime
  14.             Button1.Enabled = True
  15.             Button1.Focus()
  16.             'The call to show messagebox.
  17.             Form1.doText()
  18.         End If
  19.     End Sub
  20. End Class
Feb 10 '13 #9

100+
P: 116
Hi everyone,

Many thanks for all the effort here but I think I've located the problem and it goes way further back than I thought :\

It seems the error was that the opening of Form2 was done by creating a Dim newform2 as New Form2 and then calling newform2.Show() rather than just doing Form2.Show()

This seems to have killed the shared variables and the amended text etc, causing the error....

Didn't occur to me that this could be an issue as this is all a bit new to me but thanks for all you replies here confirming that the actual code I was attempting was fine - just now seems my opening of the form was the culprit.

Nice to find a forum with helpful, prompt and informative responses though. All the best.
Feb 11 '13 #10

IronRazer
P: 83
Hey robertybob,
It was no problem. I like to help when i can. I find that most times it seems to be something simple that i overlook all day long that ends up being the problem in my programs. Have a good day.
Feb 11 '13 #11

Post your reply

Sign in to post your reply or Sign up for a free account.