如何使Form1中label.text变化时窗体2复选框被选中? [英] How to make Form1 label.text change when checkbox on form2 is checked?

查看:103
本文介绍了如何使Form1中label.text变化时窗体2复选框被选中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的C#,我试图用2个不同形式的我的第一个实验。

I'm very new to c# and am trying my first experiments with 2 different forms.

我想使它让您拥有一个label1和在Form1上按钮1,并在Form2一个checkbox1。

I'd like to make it so you have a label1 and a button1 on Form1, and a checkbox1 on Form2.

在Form1上Button1的打开窗体2,一旦你在Form2,在label1的变化的文字。检查checkbox1

the button1 on Form1 opens Form2, and once you check checkbox1 on Form2, the text in label1 changes.

我想这有利用事件来完成,但是事件到现在真正让我困惑的唯一的事情,所以我在本质上想这个问题,更多的是事件的用法。这也是我觉得非常混乱,如果我看它在MSDN和其他网站。

I'm thinking this has to be done using events, but events are the only thing up to now that genuinely confuse me, so I guess in essence this question is more about the usage of events. Which I also find terribly confusing if I look it up on MSDN and other websites.

帮助将非常AP preciated,这让我感到非常愚蠢的。

Help would be very much appreciated, this is making me feel extremely stupid.

推荐答案

您可以从Form1的实例订阅在Form2的实例的复选​​框的事件的CheckedChanged,直接。
内部Form1上,只显示该窗体2订阅复选框的事件的CheckedChanged

You can subscribe to the event CheckedChanged of the checkbox in the Form2 instance, directly from the Form1 instance. Inside Form1, just before displaying the Form2 subscribe to the CheckedChanged event of the checkbox

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

,然后在Form1的(本)的处理程序在Form2的上调的CheckedChanged事件定义

and then define in Form1 (this) the handler for the checkedChanged event raised in Form2

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

对于这个工作,你需要的财产修饰符上的复选框从私人到<$ C $改变C>公开

在这样的窗体2并不需要知道,有一个Form1并每次有人点击你需要以另一种形式来改变标签的复选框。改变其内部状态(标签上的文字)人的职责是在Form1上谁已通知其要求的系统。

In this way your Form2 doesn't need to know that there is a Form1 and that every time someone clicks on the checkbox you need to change a label in another form. The responsability to change its internal state (the text on the label) is on Form1 who has notified the system of its requirements.

这篇关于如何使Form1中label.text变化时窗体2复选框被选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆