❔ Get Informations of Form 1 in Form 2

Hello, I want to copie the Labeltext in Form 1 with the name "LinieValue" to Form2 inLabel4. How can i do this?
11 Replies
Anu6is
Anu6is15mo ago
you can pass the data in the constructor of Form2 (if some action in form1 opens form2) you could also have a public property in form2 that you can set from wherever the form is being shown
Pobiega
Pobiega15mo ago
Its probably easier to just pass a reference of the form itself to the other form
nichtfamedominik
nichtfamedominikOP15mo ago
how can i do that? im completly new so
Pobiega
Pobiega15mo ago
how do you create your Form2 instance? and where
nichtfamedominik
nichtfamedominikOP15mo ago
i created form 2 via project direcly and via a button Form1 closes and form2 opens this.Hide(); Form2 form2 = new Form2(); form2.ShowDialog(); this.Close(); like this
Pobiega
Pobiega15mo ago
okay change the second line to Form2 form2 = new Form2(this); and update the form2 constructor to match you now have a reference to form1 in form2
nichtfamedominik
nichtfamedominikOP15mo ago
so in form 2 i do? EDIT: Got it and how can i get with this a Text of a Label in Form1 to Form2?
Pobiega
Pobiega15mo ago
well, you can now access things from form1 in form2 via that reference so you'd make a public property that exposes the data you need from form1, then just access that in form2
nichtfamedominik
nichtfamedominikOP15mo ago
so in a TextChanged Event i write "Form2.Label4.Text = StreckeValue.Text"?
Pobiega
Pobiega15mo ago
I don't know what your code looks like my dude, I have no idea
public class Form1
{
private Textbox _textBox1 = new();

public string TextBox1Value => _textBox1.Value;
}

public class Form2
{
private readonly Form1 _form1;

private Label _label1 = new();

public Form2(Form1 form1)
{
_form1 = form1;
}

public void Whatever()
{
_label1.Text = _form1.TextBox1Value;
}
}
public class Form1
{
private Textbox _textBox1 = new();

public string TextBox1Value => _textBox1.Value;
}

public class Form2
{
private readonly Form1 _form1;

private Label _label1 = new();

public Form2(Form1 form1)
{
_form1 = form1;
}

public void Whatever()
{
_label1.Text = _form1.TextBox1Value;
}
}
something like this.
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server