This code demonstrates how you can pass data from one form to another using a delegate. The advantage of using a delegate is that the form from which you want to send the data, doesn't need to know anything about the form that it's sending its data to. This way, you can reuse the same form in other forms or applications.
frm.num = 你要传的值;
frm.show();这样不就可以传过去了么,num在newfrm里面是public的
this.textbox1.text = thisowner.aa;
是不是传过值以后,子窗体又执行了什么初始化代码,将值还原成null 了
public string strChild;
...load(...)
{
FormParent formParent = new FormParent();
strChild = formParent.strParent;
}2.父窗体赋值给子窗体
public string strParent;
...click(...)
{
FormChild formChild = new FormChild();
formChild.strChild = strParent;
}两种方法都试过了,单步跟踪后到子窗体的值都是NULL
要不用委托在两窗体间传值。
要不在new form2的时候把form1直接传过去,然后在form2的构造函数里拿到form1,在拿form1里的值
//父窗口调子窗口函数
private void ShowLinkDBDialog(object sender, EventArgs e)
{
//连接子对话框。
fchild obj = new fchild(this);//this父窗口
obj.WindowState = FormWindowState.Normal;
obj.ShowDialog();
this.TextBox1.Text = obj.RetValue;
}
子窗口相关内容
private Form _parentForm=null;
private static string _RetValue = null;
public string RetValue
{
get { return _RetValue; }
}
//
public fVistSQLServer(Form parentForm)//带参数的构造函数
{
InitializeComponent();
this._parentForm = parentForm;
}
//点确定按钮,返回信息到父窗口,并关闭子窗口.
private void confirm_Click(object sender, EventArgs e)
{
_RetValue = this.TextBox1.Text.Trim();
((fparent)_parentForm).TextBox2.Text="123"; //关闭窗口
this.Close();
}
注意:以上代码包含了两种父窗口传值给子窗口的过程。
C# codepublic string strParent;
...click(...)
{
FormChild formChild = new FormChild();
formChild.strChild = strParent;
}
修改一下你上面的代码
//--------------------
在子窗体中这样写:
private string _strChild;
public string strChild
{
set{_strChild=value;}
}父窗口传值给子窗口...click(...)
{
FormChild formChild = new FormChild();
formChild._strChild = strParent;//不知道你这个strParent的值是否存在,还是为NULL.
formChild .MdiParent = FormParent.ActiveForm;//这句可有可无,根据情况而定
formChild .WindowState = FormWindowState.Maximized;
formChild .Show();
}
//父窗口调子窗口函数 private void ShowLinkDBDialog(object sender, EventArgs e)
{
//连接子对话框。
fchild obj = new fchild(this);//this父窗口
obj.WindowState = FormWindowState.Normal;
obj.ShowDialog();
this.TextBox1.Text = obj.RetValue;
}
子窗口相关内容 private Form _parentForm=null;
private static string _RetValue = null;
public string RetValue
{
get { return _RetValue; }
}
//
public fVistSQLServer(Form parentForm)//带参数的构造函数
{
InitializeComponent();
this._parentForm = parentForm;
}
//点确定按钮,返回信息到父窗口,并关闭子窗口. private void confirm_Click(object sender, EventArgs e)
{
_RetValue = this.TextBox1.Text.Trim();
((fparent)_parentForm).TextBox2.Text="123"; //关闭窗口
this.Close();
}
注意:以上代码包含了两种父窗口传值给子窗口的过程。
//父窗口调子窗口函数
C# code
private void ShowLinkDBDialog(object sender, EventArgs e)
{
//连接子对话框。
fchild obj = new fchild(this);//this父窗口
obj.WindowState = FormWindowState.Normal;
obj.ShowDialog();
this.TextBox1.Text = obj.RetValue;
} 子窗口相关内容
C# code
private Form _parentForm=null;
private static string _RetValue = null;
public string RetValue
{
get { return _RetValue; }
}
//
public fchild(Form parentForm)//带参数的构造函数
{
InitializeComponent();
this._parentForm = parentForm;
}//点确定按钮,返回信息到父窗口,并关闭子窗口.
C# code
private void confirm_Click(object sender, EventArgs e)
{
_RetValue = this.TextBox1.Text.Trim();
((fparent)_parentForm).TextBox2.Text="123"; //关闭窗口
this.Close();
}
注意:以上代码包含了两种父窗口传值给子窗口的过程。2.父窗体赋值给子窗体 在子窗体中这样写: C# code
private string _strChild;
public string strChild
{
set{_strChild=value;}
}
父窗口传值给子窗口
C# code
...click(...)
{
FormChild formChild = new FormChild();
formChild._strChild = strParent;//不知道你这个strParent的值是否存在,还是为NULL.
formChild .MdiParent = FormParent.ActiveForm;//这句可有可无,根据情况而定
formChild .WindowState = FormWindowState.Maximized;
formChild .Show();
}
2 在子窗口:
public static Form1 fm1 = null;
。
。
。
public Form1()
{
InitializeComponent();
fm1 = this;
}
父窗口就访问了
Form1.fm1.*****
代码如下:
public string curr_value
{
get {return _curr_value;}
set { value;}
}2.父窗体代码如下:
private FormChild frm = null;
frm = new FromChild();
frm.curr_value = "传递的值";