在Form1中按下按钮后更改Form2中的标签文本 [英] Changing label text in Form2 after pressing a button in Form1
问题描述
例如,我希望Form2中的标签文本更改为Button 1被按下,如果我在Form1中按下button1,并且如果我在Form1中按下button2,它将
变成按钮2被按下。。
注意:
Form1和Form2不会同时显示。所以我不得不点击按钮,然后Form2会显示更新后的标签文本。 您可以添加一个事件点击Form1类上的button1
private void button1_Click(object sender,EventArgs e)
{
Form2 form = new Form2();
form.Show();
//如果你想隐藏form1
// this.Hide();
form.label1.Text =Hello World;
}
但在此之前,您应该将您的label1标记为公共Form2.Designer .cs:
public System.Windows.Forms.Label label1;
How do I change a label text in Form2 after clicking a button in Form1?
For example, I want the label text in Form2 to change to "Button 1 was pressed" if I pressed button1 in Form1 and if i pressed button2 in Form1, it will be "Button 2 was pressed".
Note: Form1 and Form2 are not shown at the same time. So I would have to click the button and then Form2 will show up with the updated label text.
You can add an event click on button1 on your Form1 class
private void button1_Click(object sender, EventArgs e)
{
Form2 form= new Form2();
form.Show();
// if you want to hide form1
// this.Hide();
form.label1.Text = "Hello World";
}
But before that you should make your label1 marked as public on your Form2.Designer.cs :
public System.Windows.Forms.Label label1;
这篇关于在Form1中按下按钮后更改Form2中的标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!