473,834 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

changing label in another form

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 string is used to change the text in the label control on the frmMain form.

Any help would be appreciated,
thanks!
Nov 16 '05 #1
5 10542
Sambo wrote:
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 string is used to change the text in the label control on the frmMain form.

Any help would be appreciated,
thanks!


in frmMain, delcalare your label as a global variable (outside of any method)
You have to give it public access rights.

public Label myLabel; //or something among those lines

In frmSettings to change the test of label, do this.

frmMain.myLabel .Text = "Your string.";

And you're done.

Hope this helps.
Nick Z.
Nov 16 '05 #2
"=?Utf-8?B?U2FtYm8=?=" <Sa***@discussi ons.microsoft.c om> wrote in
news:B0******** *************** ***********@mic rosoft.com:
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 string is used to change the text in the label control on the
frmMain form.

Any help would be appreciated,
thanks!


Start a new windows application.
Add a second form to the project - Form2

Add a button to Form1
Add a label to Form1

Add a button to Form2
Add a private variable to Form2:

private Form1 parentForm;

Create a constructor overload in Form2

public Form2(Form1 parentForm)
{
InitializeCompo nent();
myParent = parentForm;
}

Add the following code to the Click event of the button on Form1:

private void button1_Click(o bject sender, System.EventArg s e)
{
Form2 myForm = new Form2((Form1) this);

myForm.Show();

}

Add the following code to the Click event of the button on Form2:

private void button1_Click(o bject sender, System.EventArg s e)
{
//assuming you can be sure of the indes of
//the control in the controls collection
//which might change if you modify the form
myParent.Contro ls[0].Text = "Hello World";

//or to be sure you have to right control
//no matter what happens
foreach(Control mycontrol in myParent.Contro ls)
{
if(mycontrol.Ge tType() == typeof(Label))
{
if(mycontrol.Na me == "label1")
{
mycontrol.Text = "Hello Again";
}
}
}
}

Cheers,
Dave

Nov 16 '05 #3
Nick <pa*****@optonl ine.net> wrote in
news:gW******** ************@ne ws4.srv.hcvlny. cv.net:
Sambo wrote:
I have two forms, frmMain and frmSettings. I have a label in frmMain
that i would like to change from frmSettings.
in frmMain, delcalare your label as a global variable (outside of any
method) You have to give it public access rights.

public Label myLabel; //or something among those lines


You naughty boy Nick. You've just broken encapsulation!

:)

To paraphrase Mommy Dearest:
No More Public Fields...EVER!

Dave
Nov 16 '05 #4
David Totzke (.NET/C# MVP) wrote:
Nick <pa*****@optonl ine.net> wrote in
news:gW******** ************@ne ws4.srv.hcvlny. cv.net:

Sambo wrote:

I have two forms, frmMain and frmSettings. I have a label in frmMain
that i would like to change from frmSettings.


in frmMain, delcalare your label as a global variable (outside of any
method) You have to give it public access rights.

public Label myLabel; //or something among those lines

You naughty boy Nick. You've just broken encapsulation!

:)

To paraphrase Mommy Dearest:
No More Public Fields...EVER!

Dave


Hehe. I dont see how its harmful in any way though.
Could you take the time to exaplain.

Thanks.
Nick Z.
Nov 16 '05 #5
Nick <pa*****@optonl ine.net> wrote in news:aEvMc.8390 $YK2.2126732
@news4.srv.hcvl ny.cv.net:
You naughty boy Nick. You've just broken encapsulation!

:)

To paraphrase Mommy Dearest:
No More Public Fields...EVER!

Dave


Hehe. I dont see how its harmful in any way though.
Could you take the time to exaplain.


In this instance it's not really a bad thing but from an Object Oriented
perspective, you should never allow access to internal fields unless you
use an accessor method or property. This allows you control over what
happens to your object's state.

So, in the case of the label, you could add a public property to set the
text value of the label. Here are some examples:

//example accessor methods (this is Java style)
public void SetLabel1Text(s tring theText)
{
//i can test the value here
//and raise an exception if I
//want to or just deal with it here
if(value.Length > 10)
{
label1.Text = value.Substring (1,10);
}
else
{
label1.Text = value;
}
}

public string GetLabel1Text()
{
return label1.Text;

}

//example property
public string Label1Text
{
get
{
return label1.Text;
}

set
{
//i can test the value here
//and raise an exception if I
//want to or just deal with it here
if(value.Length > 10)
{
label1.Text = value.Substring (1,10);
}

}
}
Cheers,
Dave
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1416
by: Bob Achgill | last post by:
Label example: "See the dog run." I would like to be able to highlight a given word in the example label using another color than the rest of the words in the sentence and/or maybe underline the highlighted word. Said another way...Does labels support in-line font/color tags for setting text properties such as HTML does?
3
2691
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 forms1 I've placed two components a NotifyIcon and FileSystemWatcher. I created a event handler called onchanged which responds to file being created in a certain directory. Now my problem is I can't figure out how to change the label.text...
3
1592
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 and programming. I just bought 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 forms1 I've placed two components a NotifyIcon and FileSystemWatcher. I...
7
2980
by: Sakharam Phapale | last post by:
Hi All, How to preserve the old font properties while changing new one? I posted same question 2 months back, but I had very small time then. eg. "Shopping for" is a text in RichTextBox and already formatted as "Shopping" ---Bold "for" -----Regular Now I want to underline whole text by preserving old style i.e. Bold and
6
9168
by: Roger Ries via DotNetMonster.com | last post by:
I'm trying to change the text in a label from another form. lblInfo.Text = "ABC" Works for the form your currently in but how the heck do you change that label information from another form. Later --
6
6880
by: AMP | last post by:
Hello, I have an mdi program with a child form ("A") and another child ("B"). I want to change the text of a label on A by changing an item from a combobox on B. I can code the comboBox1_SelectedIndexChanged that is on B but I dont know how to send that info to the label on A.I cant seem to access A from B. Help Thanks Mike
4
1315
by: Monty | last post by:
Hi there, From one page, I have created a dropdown list of employees. When an employee is selected, I would like another page to open, based upon the employee. Here is my question... How do I change the datasource.id of a grid on another page as it is opening? Thank you, Mark
6
2860
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 font of Arial and 8.25pt. Great.
0
9643
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10789
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10504
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10544
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7755
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5790
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.