winForm中两个窗口传值的方法
今天做c++程序时,想起了.net中winform中两个窗口传值的问题.
Cwnd* m_pParent;
CRegisterDialog::CRegisterDialog(CWnd* pParent /*=NULL*/)
: CDialog(CRegisterDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CRegisterDialog)
. . . . . .
//}}AFX_DATA_INIT
m_pParent=pParent; //在下面的c#代码中我采用同样的方法!
}
1..net中父窗口传给子窗口值的各种方法就不用说了
2.关于子窗口向父窗口传值
以前的做法是用sendmessage(),或者将父窗口中的成员设置为static类型,今天想到了另一种方法:
举例,子窗口更新父窗口中文本框的值
Form1 中 (父窗口:)
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnOpen;
public System.Windows.Forms.TextBox txtContent; //注意是public
........
........
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnOpen_Click(object sender, System.EventArgs e)
{
Form2 frm=new Form2(this);
frm.ShowDialog();
}
}
Form2中(子窗口)
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txtValue;
private Form _parentForm=null;
public Form2()
{
InitializeComponent();
}
public Form2(Form parentForm)
{
InitializeComponent();
this._parentForm =parentForm;
}
........
........
//更新父窗口中文本框中的值!
private void button1_Click(object sender, System.EventArgs e)
{
((Form1)_parentForm).txtContent.Text =this.txtValue .Text ;
}
}
//以上算是为winform中各种窗口间传值又增加了一种新的方法!