当前位置:K88软件开发文章中心编程语言.NET.NET01 → 文章内容

窗体Form之间传值方法

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2018-12-31 11:36:49

:2010-09-12 09:12:00

怎样从一个form传递数据到另一个form?假设Form2的数据要传到Form1的TextBox。

在Form2:

// Define delegate

public delegate void SendData(object sender);

// Create instance

public SendData sendData;

在Form2的按钮单击事件或其它事件代码中:

if(sendData != null)
       {
           sendData(txtBoxAtForm2);
       }

this.Close(); //关闭Form2

在Form1的弹出Form2的代码中:

Form2 form2 = new Form2();

form2.sendData = new Form2.SendData(MyFunction);

form2.ShowDialog();

private void MyFunction(object sender)
        { 
               textBox1.Text = ((TextBox)sender).Text;
        }



窗体Form之间传值方法