How to Call Label From form2 to Form1 c#
我有form1一个文本框和一个按钮(检查目的)
在form2中,我有label1(名称为" Example")
检查按钮中的代码
1 2 3 4 5 6 | Form f2 = new Form2(); if (textbox.text != f2.label1) { MessageBox.Show("textbox Did Not Match to label") } else MessageBox.Show("textbox Match to label!") |
将此添加到Form2类:
1 2 3 4 5 6 7 8 9 | public partial class Form2 : Form { public Form2() { InitializeComponent(); } public string Label1Text => label1.Text; } |
然后:
1 2 3 4 5 6 | Form f2 = new Form2(); if (textbox.text != f2.Label1Text) { MessageBox.Show("textbox Did Not Match to label") } else MessageBox.Show("textbox Match to label!") |
或创建一个公共方法:
1 2 3 4 5 6 7 8 9 10 11 | public partial class Form2 : Form { public Form2() { InitializeComponent(); } public string GetLabel1Text() { return label1.Text; } } |
然后:
1 2 3 4 5 6 | Form f2 = new Form2(); if (textbox.text != f2.GetLabel1Text()) { MessageBox.Show("textbox Did Not Match to label") } else MessageBox.Show("textbox Match to label!") |