简体   繁体   English

c#用不同的形式用按钮更改标签文本

[英]c# changing label text with button in a different form

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 在其中,我只显示了一些小东西来显示我认为不重要的文本,但是如果是的话,请告诉我,我也将其发布

Create a property on your second Form that sets the text value on the Label: 在第二个窗体上创建一个属性,该属性在Label上设置文本值:

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

Then use it when you instantiate and show the Form: 然后在实例化并显示Form时使用它:

SingleChart Form_SC = new SingleChart();
Form_SC.Name = "Danny";
Form_SC.Show();

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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM