0

So for school I have this project I'm making. In it I want to create text in my second form by pressing on a button in my first form. Showing the second form by pressing the button works and I also already have some text in the second form. But like I already said, how do I add (or change, either is fine) text from the first form?

Here's my code:

    BtnNor.Click += new EventHandler(NorChart); //BtnNor is the name of the button
    }
    void NorChart(object sender, EventArgs e) 
    {
        SingleChart Form_SC = new SingleChart(); //SingleChart is the name of the second form.
        Form_SC.Show();

    }

This is for opening the second form. In it I just have some small things to display text which I think isn't important, but if it is please do tell me and I'll post it too

2 Answers2

1

Create a property on your second Form that sets the text value on the Label:

public string Name
{
    set { lblName.Text = value; }
}

Then use it when you instantiate and show the Form:

SingleChart Form_SC = new SingleChart();
Form_SC.Name = "Danny";
Form_SC.Show();
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

if you want to change the label or text that is already on form you can try this code:

BtnNor.Click += new EventHandler(NorChart); //BtnNor is the name of the button
}
void NorChart(object sender, EventArgs e) 
{
    SingleChart Form_SC = new SingleChart(); //SingleChart is the name of the second form.
    Form_SC.label1.text = "2nd form label value" //This will change label 2nd form.
    Form_SC.Show();

}
bogzy
  • 116
  • 3
  • What do I do with the label1 in Form_SC.label1.text? Change it to the name of the label? Because either I'm doing that wrong, or I need to do something else – Danny Nuyten Oct 29 '16 at 12:32