如何检查form2上的复选框时Form1 label.text更改?

我对c#很新,我正在用2种不同的forms尝试我的第一次实验。

我想这样做,所以你在Form1上有一个label1和一个button1,在Form2上有一个checkbox1。

Form1上的button1打开Form2,一旦您检查Form2上的checkbox1,label1中的文本将更改。

我认为这必须使用事件来完成,但事件是迄今为止唯一让我真正迷惑的事情,所以我想这个问题实质上更多的是关于事件的使用。 如果我在MSDN和其他网站上查找,我也会发现非常混乱。

非常感谢帮助,这让我觉得非常愚蠢。

您可以直接从Form1实例订阅Form2实例中复选框的事件CheckedChanged。 在Form1内部,就在显示Form2之前订阅复选框的CheckedChanged事件

Form2 frm = new Form2(); frm.chkBox1.CheckedChanged += new EventHandler(this.ReceiveCheckedChanged); frm2.ShowDialog(); 

然后在Form1(this)中定义Form2中引发的checkedChanged事件的处理程序

 private void ReceiveCheckedChanged(object sender, EventArgs e) { CheckBox chk = sender as CheckBox; if(chk.Checked) this.label1.Text = "Checked"; else this.label1.Text = "UnChecked"; } 

要使其工作,您需要将复选框上的属性ModifiersPrivate更改为Public

通过这种方式,您的Form2不需要知道Form1,并且每当有人点击复选框时,您需要更改另一种forms的标签。 更改其内部状态(标签上的文本)的责任在Form1上,该Form1已通知系统其要求。

在这种情况下,您可以使用CheckedChanged事件:

 public void checkbox2_CheckedChanged(object sender, EventArgs e) { if (checkbox2.Checked) { Form1.Label1.Text = "Checkbox 2 has been checked"; } else { Form1.Label1.Text = ""; } } 

http://www.c-sharpcorner.com/uploadfile/mahesh/checkbox-in-C-Sharp3/

请注意,您必须更改Form1的Access Modifier并使Label1公开,以便Form2可以更改Text属性。

要执行此操作,请转到Form1 ,选择Label1 goto Properties ,选择Modifiers并从Private更改为Public 。 然后,Form2将可以访问Label

使用Controller和Events来分离表单

执行此类操作的正确方法是通过引入Controller类并使用events来指示状态更改来将两种forms相互分离。

这是一个例子。

首先,创建一个名为WindowsFormsApplication1的新默认Windows窗体应用程序,并添加两个窗体, form1form2

然后向form1添加一个名为“button1”的按钮和一个名为“label1”的标签。

然后向form2添加一个名为“checkbox1”的复选框。

form1设计器中,双击该按钮为其添加单击处理程序。

form2设计器中,双击该复选框以为其添加更改处理程序。

现在添加一个名为“Controller”的新类,并添加以下代码:

 using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { internal sealed class Controller { public void RunForm1() { _form1 = new Form1(); // The next line defines our response to the button being pressed in Form1 _form1.ButtonClicked += (sender, args) => showForm2(); Application.Run(_form1); } private void showForm2() { var form2 = new Form2(); // The next line defines our response to the checkbox changing in Form2. form2.CheckBoxChanged += (sender, args) => _form1.SetLabel("Checkbox = " + ((CheckBox)sender).Checked); form2.ShowDialog(_form1); } private Form1 _form1 ; } } 

现在将Form1.cs更改为:

 using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1: Form { // Here's where we announce our event handler to the world: public event EventHandler ButtonClicked; public Form1() { InitializeComponent(); } public void SetLabel(string text) { label1.Text = text; } private void button1_Click(object sender, EventArgs e) { // We make a copy of ButtonClicked before checking it for null because // in a multithreaded environment some other thread could change it to null // just after we checked it for nullness but before we call it, which would // cause a null reference exception. // A copy cannot be changed by another thread, so that's safe to use: var handler = ButtonClicked; if (handler != null) handler(sender, e); } } } 

并将Form2.cs更改为:

 using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form2: Form { // Here's the event handler for the check box: public event EventHandler CheckBoxChanged; public Form2() { InitializeComponent(); } private void checkBox1_CheckedChanged(object sender, EventArgs e) { var handler = CheckBoxChanged; if (handler != null) handler(sender, e); } } } 

最后,将Program.cs更改为:

 using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Controller controller = new Controller(); controller.RunForm1(); } } } 

现在运行程序并单击按钮,然后单击该复选框几次。 您将看到Form1中的标签发生变化。

通过这种方式,您已将Form1Form2完全分离,并将控制逻辑放入单独的Controller类中。

Form1中:

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var form = new Form2(); form.Changed += (o, args) => label1.Text = "some"; form.ShowDialog(); } } 

窗体2:

 public partial class Form2 : Form { public delegate void ChangedEventHandler(object sender, EventArgs e); public event ChangedEventHandler Changed; public Form2() { InitializeComponent(); } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (Changed != null) { Changed(this, e); } } } 

使用CheckBox的CheckedChanged事件。

此外,您可以查看好的教程如何在C#中使用事件。