我是c#的新手,我正在尝试使用两种不同的形式进行我的第一次实验。
我想让它在Form1上有一个label1和一个button1,在Form2上有一个checkbox1。
Form1上的button1打开Form2,一旦你检查Form2上的checkbox1,label1中的文本就会改变。
我认为这必须使用事件来完成,但事件是迄今为止真正让我困惑的事情,所以我想这个问题本质上更多的是关于事件的使用。如果我在MSDN和其他网站上查找,我也会感到非常困惑。
非常感谢帮助,这让我觉得非常愚蠢。
答案 0 :(得分:2)
在这种情况下,您可以使用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
中的访问者修饰符并公开Label1
,以便Form2
可以更改Text
属性。
要执行此操作,请转到Form1
,选择Label1
转到Properties
,选择Modifiers
并从Private
更改为Public
。然后,Form2可以访问Label
。
答案 1 :(得分:2)
使用控制器和事件解耦表单
执行此类操作的正确方法是通过引入Controller
类并使用events
来指示状态更改来将两种形式相互分离。
这是一个例子。
首先,创建一个名为WindowsFormsApplication1
的新默认Windows窗体应用,并添加两个窗体,form1
和form2
。
然后将名为“button1”的按钮和名为“label1”的标签添加到form1
。
然后在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中的标签发生变化。
通过这种方式,您完全将Form1
与Form2
分离,并将控制逻辑放入一个单独的Controller类中。
答案 2 :(得分:0)
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
事件。
此外,您可以查看好tutorial如何在C#中使用事件。
答案 3 :(得分:0)
您可以直接从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";
}
为此,您需要更改Modifiers
到Private
Public
通过这种方式,您的Form2不需要知道Form1,并且每当有人点击复选框时,您需要更改另一种形式的标签。更改其内部状态(标签上的文本)的责任在Form1上,该Form1已通知系统其要求。