winform+c#之窗体之间的传值
2008-03-31 15:27 Virus-BeautyCode 阅读(10754) 评论(4) 编辑 收藏 举报
窗体传值可以分为两类。
1、主窗体往子窗体传值
有两种方法,一种是在子窗体提供重载构造函数,利用重载构造函数传递值,适用于传值数量比较少;第二种是,在子窗体中定义一个主窗体对象,然后就可以接收到主窗体的属性值了,适用于传值数量大。
主窗体代码如下:
利用了子窗体的属性保存子窗体的值,在主窗体中可以访问到子窗体的属性
主窗体代码如下:
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();
}
}
2、从子窗体返回值到主窗体中{
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();
}
}
利用了子窗体的属性保存子窗体的值,在主窗体中可以访问到子窗体的属性
主窗体代码如下:
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();
}
}