窗體傳值可以分為兩類。
1、主窗體往子窗體傳值
有兩種方法,一種是在子窗體提供重載構造函數,利用重載構造函數傳遞值,適用于傳值數量比較少;第二種是,在子窗體中定義一個主窗體對象,然后就可以接收到主窗體的屬性值了,適用于傳值數量大。
主窗體代碼如下:
?public?partial?class?frmParent?:?Form
????{
????????private?string?strValueA?=?"";
????????public?string?StrValueA
????????{
????????????get
????????????{
????????????????return?this.strValueA;
????????????}
????????????set?{?this.strValueA?=?value;?}
????????}
????????public?frmParent()
????????{
????????????InitializeComponent();
????????}
????????private?void?button1_Click(object?sender,?EventArgs?e)
????????{
????????????this.strValueA?=?textBox1.Text;
????????????frmChild?frmchild?=?new?frmChild();
????????????frmchild.Owner?=?this;
????????????frmchild.ShowDialog();
????????????frmchild.Dispose();
????????}
????????private?void?button2_Click(object?sender,?EventArgs?e)
????????{
????????????frmChild?frmchild?=?new?frmChild(this.textBox1.Text);
????????????string?returnValue?=?"";
????????????if?(frmchild.ShowDialog()?==?DialogResult.OK)
????????????{
????????????????returnValue?=?frmchild.Str;
????????????????this.textBox1.Text?=?returnValue;
????????????}
????????}
????}
????{
????????private?string?strValueA?=?"";
????????public?string?StrValueA
????????{
????????????get
????????????{
????????????????return?this.strValueA;
????????????}
????????????set?{?this.strValueA?=?value;?}
????????}
????????public?frmParent()
????????{
????????????InitializeComponent();
????????}
????????private?void?button1_Click(object?sender,?EventArgs?e)
????????{
????????????this.strValueA?=?textBox1.Text;
????????????frmChild?frmchild?=?new?frmChild();
????????????frmchild.Owner?=?this;
????????????frmchild.ShowDialog();
????????????frmchild.Dispose();
????????}
????????private?void?button2_Click(object?sender,?EventArgs?e)
????????{
????????????frmChild?frmchild?=?new?frmChild(this.textBox1.Text);
????????????string?returnValue?=?"";
????????????if?(frmchild.ShowDialog()?==?DialogResult.OK)
????????????{
????????????????returnValue?=?frmchild.Str;
????????????????this.textBox1.Text?=?returnValue;
????????????}
????????}
????}
子窗體代碼如下:
public?partial?class?frmChild?:?Form
????{
????????private?string?str;
????????public?string?Str
????????{
????????????get?{?return?this.str;?}
????????????set?{?this.str?=?value;?}
????????}
????????private?frmParent?frmparent;
????????public?frmChild()
????????{
????????????InitializeComponent();
????????}
????????public?frmChild(string?str)
????????{
????????????this.str?=?str;
????????????InitializeComponent();
????????????this.textBox1.Text?=?str;
????????}
????????private?void?frmChild_Load(object?sender,?EventArgs?e)
????????{
????????????frmparent?=?(frmParent)this.Owner;
????????????//this.textBox1.Text?=?frmparent.StrValueA;
????????}
????????private?void?button1_Click(object?sender,?EventArgs?e)
????????{
????????????//frmparent?=?(frmParent)this.Owner;
????????????this.Str?=?this.textBox1.Text;
????????????this.DialogResult?=?DialogResult.OK;
????????????this.Close();
????????????
????????}
????}
????{
????????private?string?str;
????????public?string?Str
????????{
????????????get?{?return?this.str;?}
????????????set?{?this.str?=?value;?}
????????}
????????private?frmParent?frmparent;
????????public?frmChild()
????????{
????????????InitializeComponent();
????????}
????????public?frmChild(string?str)
????????{
????????????this.str?=?str;
????????????InitializeComponent();
????????????this.textBox1.Text?=?str;
????????}
????????private?void?frmChild_Load(object?sender,?EventArgs?e)
????????{
????????????frmparent?=?(frmParent)this.Owner;
????????????//this.textBox1.Text?=?frmparent.StrValueA;
????????}
????????private?void?button1_Click(object?sender,?EventArgs?e)
????????{
????????????//frmparent?=?(frmParent)this.Owner;
????????????this.Str?=?this.textBox1.Text;
????????????this.DialogResult?=?DialogResult.OK;
????????????this.Close();
????????????
????????}
????}
js怎樣跨頁面傳遞數據。2、從子窗體返回值到主窗體中
利用了子窗體的屬性保存子窗體的值,在主窗體中可以訪問到子窗體的屬性
主窗體代碼如下:
?public?partial?class?frmParent?:?Form
????{
????????private?string?strValueA?=?"";
????????public?string?StrValueA
????????{
????????????get
????????????{
????????????????return?this.strValueA;
????????????}
????????????set?{?this.strValueA?=?value;?}
????????}
????????public?frmParent()
????????{
????????????InitializeComponent();
????????}
????????private?void?button2_Click(object?sender,?EventArgs?e)
????????{
????????????frmChild?frmchild?=?new?frmChild(this.textBox1.Text);
????????????string?returnValue?=?"";
????????????if?(frmchild.ShowDialog()?==?DialogResult.OK)
????????????{
????????????????returnValue?=?frmchild.Str;
????????????????this.textBox1.Text?=?returnValue;
????????????}
????????}
????}
????{
????????private?string?strValueA?=?"";
????????public?string?StrValueA
????????{
????????????get
????????????{
????????????????return?this.strValueA;
????????????}
????????????set?{?this.strValueA?=?value;?}
????????}
????????public?frmParent()
????????{
????????????InitializeComponent();
????????}
????????private?void?button2_Click(object?sender,?EventArgs?e)
????????{
????????????frmChild?frmchild?=?new?frmChild(this.textBox1.Text);
????????????string?returnValue?=?"";
????????????if?(frmchild.ShowDialog()?==?DialogResult.OK)
????????????{
????????????????returnValue?=?frmchild.Str;
????????????????this.textBox1.Text?=?returnValue;
????????????}
????????}
????}
子窗體代碼如下:
public?partial?class?frmChild?:?Form
????{
????????private?string?str;
????????public?string?Str
????????{
????????????get?{?return?this.str;?}
????????????set?{?this.str?=?value;?}
????????}
????????private?frmParent?frmparent;
????????public?frmChild()
????????{
????????????InitializeComponent();
????????}
?????????private?void?frmChild_Load(object?sender,?EventArgs?e)
????????{
????????????frmparent?=?(frmParent)this.Owner;
????????????//this.textBox1.Text?=?frmparent.StrValueA;
????????}
????????private?void?button1_Click(object?sender,?EventArgs?e)
????????{
????????????//frmparent?=?(frmParent)this.Owner;
????????????this.Str?=?this.textBox1.Text;
????????????this.DialogResult?=?DialogResult.OK;
????????????this.Close();
????????????
????????}
????}
????{
????????private?string?str;
????????public?string?Str
????????{
????????????get?{?return?this.str;?}
????????????set?{?this.str?=?value;?}
????????}
????????private?frmParent?frmparent;
????????public?frmChild()
????????{
????????????InitializeComponent();
????????}
?????????private?void?frmChild_Load(object?sender,?EventArgs?e)
????????{
????????????frmparent?=?(frmParent)this.Owner;
????????????//this.textBox1.Text?=?frmparent.StrValueA;
????????}
????????private?void?button1_Click(object?sender,?EventArgs?e)
????????{
????????????//frmparent?=?(frmParent)this.Owner;
????????????this.Str?=?this.textBox1.Text;
????????????this.DialogResult?=?DialogResult.OK;
????????????this.Close();
????????????
????????}
????}