2个表单进行通信c#

时间:2011-04-27 08:01:02

标签: c# windows

您好我在应用程序上有两个表单,并且想要使用一个表单以另一种形式设置标签,我环顾四周寻找答案,但我没有得到它的工作。代码:

表1

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void uit_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2(this);
            frm.Show();
        }

        public string LabelText
        {
            get { return uit.Text; }
            set { uit.Text = value; }
        }

    }

表格2:

public partial class Form2 : Form
{
    private Form1 mainForm = null;
    public Form2(Form callingForm)
    {
        mainForm = callingForm as Form1;
        InitializeComponent();
    }
    private void Form2_Load(object sender, EventArgs e)
    {
        txtmessage.Text = this.mainForm.LabelText;
    }
    public void button1_Click(object sender, EventArgs e)
    {
        this.mainForm.LabelText = txtmessage.Text;
    }
}

有人可以解释为什么这不起作用吗?

1 个答案:

答案 0 :(得分:0)

我刚刚构建它并且工作正常。我假设你已经把事件搞定了?