changing label in another form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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!
 
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)
{
InitializeComponent();
myParent = parentForm;
}

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

private void button1_Click(object sender, System.EventArgs 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(object sender, System.EventArgs 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.Controls[0].Text = "Hello World";

//or to be sure you have to right control
//no matter what happens
foreach(Control mycontrol in myParent.Controls)
{
if(mycontrol.GetType() == typeof(Label))
{
if(mycontrol.Name == "label1")
{
mycontrol.Text = "Hello Again";
}
}
}
}

Cheers,
Dave
 
Sambo said:
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.
 
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
 
David said:
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.
 
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(string 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
 
Back
Top