4

我对 c# 很陌生,我正在尝试用 2 种不同的形式进行第一次实验。

我想让它在 Form1 上有一个 label1 和一个 button1,在 Form2 上有一个 checkbox1。

Form1 上的 button1 打开 Form2,一旦您选中 Form2 上的 checkbox1,label1 中的文本就会更改。

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

非常感谢您的帮助,这让我感到非常愚蠢。

4

4 回答 4

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/

请注意,您必须更改访问修饰符Form1Label1公开,以便Form2更改Text属性。

为此,请 goto Form1、选择Label1goto Properties、选择Modifiers并从Privateto更改Public。然后 Form2 将有权访问Label.

于 2013-03-24T23:06:38.137 回答
2

使用控制器和事件来解耦表单

做这种事情的正确方法是通过引入一个Controller类来将两种形式相互解耦,并使用events来表示状态变化。

这是一个例子。

首先,创建一个新的默认 Windows 窗体应用程序WindowsFormsApplication1,并添加两个窗体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 类中。

于 2013-03-24T23:33:12.853 回答
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复选框上的属性从更改PrivatePublic

通过这种方式,您的 Form2 不需要知道有一个 Form1 并且每次有人单击复选框时,您都需要更改另一个表单中的标签。更改其内部状态(标签上的文本)的责任在于 Form1,它已将其要求通知系统。

于 2013-03-24T23:20:56.080 回答
0

表格1:

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);
        }
    }
}

使用CheckedChangedCheckBox 的事件。

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

于 2013-03-24T23:08:13.077 回答