您的位置:首页 > 其它

Winform子窗体刷新父窗体

2016-03-03 10:08 393 查看
调用窗体(父):Form1,被调用窗体(子):Form2
方法1: 所有权法
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
//在调用Form2时,要把Form2的所有者设为Form1
Form2 f2 = new Form2() ;
f2.Owner = this;
f2.ShowDialog() ;
//Form2:
//在需要对其调用者(父)刷新时
Form1 f1 ;
f1 = (Form1)this.Owner;
f1.Refresh_Method() ;
方法2:自身传递法
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
Form2 f2 = new Form2() ;
f2.ShowDialog(this) ;
//Form2:
private Form1 p_f1;
public Form2(Form1 f1)
{
InitializeComponent();
p_f1 = f1;
}
//刷新时
p_f1.Refresh_Method() ;
方法3:属性法
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
//调用时
Form2 f2 = new Form2() ;
f2.P_F1 = this;
f2.Show() ;

//Form2:
private Form1 p_f1;
public Form1 P_F1
{
get{return p_f1;}
set{p_f1 = value;}
}
//刷新时
p_f1.Refresh_Method() ;
方法4:委托法
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
//调用时
Form2 f2 = new Form2() ;
f2.ShowUpdate += new DisplayUpdate(Refresh_Method) ;
f2.Show() ;
//Form2:

//声明一个委托
public delegate void DisplayUpdate();
//声明事件
public event DisplayUpdate ShowUpdate;
//刷新时,放在需要执行刷新的事件里
ShowUpdate(); 最好用方法四

子窗体向父窗体传值以及父窗体向子窗体传值
Code
1: 所有权法
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//
}
//在调用Form2时,要把Form2的所有者设为Form1
Form2 f2 = new Form2() ;
f2.Owner = this;
f2.ShowDialog() ;
//Form2:
//在需要对其调用者(父)刷新时
Form1 f1 ;
f1 = (Form1)this.Owner;
f1.Refresh_Method() ;

eg:

Form1中的函数:

public void DiaoYong(string str)
{
this.textBox1.Text =str;
}

private void button2_Click(object sender, EventArgs e)
{

string str = this.textBox1.Text;
Form2 f2 = new Form2(str);//在构造函数中,向子窗体传值。
f2.Owner = this;
f2.ShowDialog();
}

Form2中的函数:

public Form2(string ss)
{
InitializeComponent();
this.textBox1.Text = ss;

}

private void button1_Click(object sender, EventArgs e)
{
string st = textBox1.Text;

Form1 f1;
f1 = (Form1)this.Owner;
f1.DiaoYong(st);

this.Close();
}

如何在winform中子窗体提交数据后刷新父窗体中的DataGRIDVIEW数据?

如何在子窗体刷新父窗体的datagridview

父窗体中有个datagridview,
方法datashow为它绑定数据
子窗体是从父窗体中创建的,是用来新建数据的
在子窗体里有个保存按钮
请问如何在按钮事件中激发父窗体的datashow方法来给datagridview重新绑定数据
看了资料说要用 委托
请哪位高手给写个代码
在 子窗口里 定义事件
public class childform
{
public event EventHandler refreshData = null;

protect void OnRefreshData()
{
if( refreshData != null )
{
refreshData( this , eventArgs.Emptys);
}
}

private void Save()
{
//保存代码
//刷新父窗口
OnRefreshData();
}
}

//在父窗口里
public class ParentForm
{
private void Add()
{
ChildForm form = new ChildForm();
form.refreshData += new refreshData ( refreshData );
form.ShowDialog();
}
private void refreshData()
{
//刷新Datagridview代码
........
}
}

C# WinForm 父窗体 子窗体 传值

本次示例效果如下:
Form1为父窗体(包含textBox1、button1)
Form2为子窗体(包含textBox2、button2)

父窗体给子窗体传值
==================
1.点击Form1的button1 打开Form2
父窗体给子窗体传值 可以调用重载子窗体的构造函数 直接传入相关数值

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this.textBox1.Text);
frm2.Show();
}
}

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

public Form2(string strTextBox1Text)
{
InitializeComponent();
this.textBox2.Text = strTextBox1Text;
}
}

2.点击Form1的button1 打开Form2
并调用子窗体Form2的公开属性或方法 将Form1的textBox1的值设置给Form2的textBox2

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.TextBox2Text = this.textBox1.Text;
frm2.Show();
}
}

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

public string TextBox2Text
{
set { this.textBox2.Text = value; }
get { return this.textBox2.Text; }
}
}

3.点击Form1的button1 打开Form2
在Form2_Load调用父窗体Form1的公开属性或方法 将Form1的textBox1的值设置给Form2的textBox2

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public string TextBox1Text
{
set { this.textBox1.Text = value; }
get { return this.textBox1.Text; }
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show(this);//或 frm2.ShowDialog(this);

////或者
//Form2 frm2 = new Form2();
//frm2.Owner = this;
//frm2.Show();//或 frm2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
this.textBox2.Text = frm1.TextBox1Text;
}
}

子窗体给父窗体传值
==================
4.点击Form1的button1 打开Form2
再点击Form2的button2
在button2_Click事件中 通过this.Owner将Form2的textBox2的值设置给Form1的textBox1
并关闭Form2

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show(this);//或 frm2.ShowDialog(this);

////或者
//Form2 frm2 = new Form2();
//frm2.Owner = this;
//frm2.Show();//或 frm2.ShowDialog();
}
}

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
//注意 如果textBox1是放在panel1中的 则先找panel1 再找textBox1
((TextBox)frm1.Controls["textBox1"]).Text = this.textBox2.Text;
this.Close();
}
}

5.点击Form1的button1 打开Form2
再点击Form2的button2
在button2_Click事件中 通过this.Owner及调用父窗体Form1的公开属性或方法
将Form2的textBox2的值设置给Form1的textBox1
并关闭Form2

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public string TextBox1Text
{
set { this.textBox1.Text = value; }
get { return this.textBox1.Text; }
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show(this);//或 frm2.ShowDialog(this);

////或者
//Form2 frm2 = new Form2();
//frm2.Owner = this;
//frm2.Show();//或 frm2.ShowDialog();
}
}

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
frm1.TextBox1Text = this.textBox2.Text;
this.Close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: