Reputation: 11
What i'm trying to do is sending from a class a value to a label inside Form1. Form1 is already running, so i can't say something like Form1.Show("value"); but i want something like this:
Form1 frm1 = new Form1();
frm1.label1.text = "Hello World"; //<-- i want to send this.
In VB is it this:
Form1.Label1.Text = "Hello World";
So can this be done in a simple way? And without big code?
Upvotes: 1
Views: 72
Reputation: 331
Just create a method that has a return value like:
Public string SettingTheLabel()
{
//something here
return "LabelText";
}
Then set your label text on the containing form to the method
Label1.Text = SettingTheLabel();
Upvotes: 0
Reputation: 146
Add a public property to your form, and use that property to set the form's label.
public string LabelText {
get { return Label1.Text; }
set { Label1.Text = value; }
}
And then from the class:
Form1.LabelText = "Hello World";
Upvotes: 0
Reputation: 61379
Disclaimer: This is probably a sign you are going down the wrong path.
That said, you can't do this because controls are private members. However, you can expose them through a property:
//Form1.cs
public Label SuperLabel { get { return label1; } }
...
//Other.cs
frm1.SuperLabel.Text = "Hello World";
Even better would be a method
//Form1.cs
public void SetSuperText(string text)
{
label1.Text = text;
}
...
//Other.cs
frm1.SetSuperText("Hello World");
Upvotes: 2