【原】子窗体传值给父窗体
父窗体代码
using System;
using System.Windows.Forms;
namespace 子窗体传值给父窗体
{
public partial class formFather : Form
{
public formFather()
{
InitializeComponent();
}
//封装一个属性
public string str { get; set; }
private void button1_Click(object sender, EventArgs e)
{
formSun sun = new formSun();//实例化子窗体
sun.Owner = this;//指定子窗体的父窗体
sun.ShowDialog();//弹出子窗体
this.label1.Text = str;//给label1赋值
}
}
}
using System.Windows.Forms;
namespace 子窗体传值给父窗体
{
public partial class formFather : Form
{
public formFather()
{
InitializeComponent();
}
//封装一个属性
public string str { get; set; }
private void button1_Click(object sender, EventArgs e)
{
formSun sun = new formSun();//实例化子窗体
sun.Owner = this;//指定子窗体的父窗体
sun.ShowDialog();//弹出子窗体
this.label1.Text = str;//给label1赋值
}
}
}
子窗体的代码
using System;
using System.Windows.Forms;
namespace 子窗体传值给父窗体
{
public partial class formSun : Form
{
public formSun()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
formFather Father = (formFather)this.Owner;//实例化父窗体,但这里的实例化有些不一样哦,
Father.str = this.textBox1.Text;//把text里的值赋值给父窗体中的str属性
this.Close();//关闭子窗体本身
}
}
}
using System.Windows.Forms;
namespace 子窗体传值给父窗体
{
public partial class formSun : Form
{
public formSun()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
formFather Father = (formFather)this.Owner;//实例化父窗体,但这里的实例化有些不一样哦,
Father.str = this.textBox1.Text;//把text里的值赋值给父窗体中的str属性
this.Close();//关闭子窗体本身
}
}
}