473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem changing label text from different form

116 64KB
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
10 4947
robertybob
116 64KB
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
83 64KB
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
HI robertybob,

if you have example please share with us because i have also face this problem.
Feb 9 '13 #4
robertybob
116 64KB
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
robertybob
116 64KB
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
83 64KB
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
robertybob
116 64KB
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
83 64KB
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
robertybob
116 64KB
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
83 64KB
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

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

Similar topics

5
by: Sambo | last post by:
I have two forms, frmMain and frmSettings. I have a label in frmMain that i would like to change from frmSettings. So when the user clicks the command button in frmSettings then a value from a...
1
by: Alpha | last post by:
I'm working with a Window based application. I have a small form with just 1 label control which is set as Enabled and Visible. Its text is in black telling users to please wait while application...
3
by: Mike Johnson | last post by:
I'm new to VB.Net and programming. I just brought VB.Net Standard I'm working on a small program for work. I've created two forms the first is named Forms1 and the second is named SettingsForm on...
3
by: Mike Johnson | last post by:
Thanks for the quick responses. I'm having trouble understanding. I've included the code I using. perhaps someone can tell me what I'm doing wrong. My original question was, I'm new to VB.Net...
5
by: RD | last post by:
Form1 calls a sub that is public in module1. How do you write a value to a text property from with this sub? Eg Module myModule sub mysub() 'From here how do I write to the label1.text...
4
by: Allen Smith | last post by:
When the user clicks the download button I am changing the text of a label control and do download operation. The download part of the code makes the application appears like stop responding. ...
4
by: darnnews | last post by:
When a user selects a "Medium" in a form I have made, I would like to change a Label on the form. Can I change a label based on an update of a listbox? This code does not work: Case "Print"...
0
by: Ron | last post by:
Hi All, If utilized as a sub-form, the ControlTip Texts of all the labels (you know...you can hover the mouse over either the control itself, or the label for that control) flickers like crazy. ...
6
by: andrew.ames | last post by:
Hi I have a pretty basic windows application created in Visual Studio 2005 and VB.NET. I set my Form's font to Arial 8.25pt, so when i added a label and a button they automatically have a...
2
by: Fred | last post by:
I have two forms, the first having a label and a button that opens the second form. The second form has a button that I want to set text in form1's label before closing. I have no problem getting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.