第一种方法: 用委托,frmFather和frmSun是同一组 frmFather
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Base 11 { 12 public partial class frm_BaseManage : Form 13 { 14 15 //........省略 16 private void dgvBaseDetails_Blind(int BaseDetails) 17 { 18 //绑定数据到DataGridView 19 dt_BaseDetails = BLL_Base.frm_Base.SelectBaseDetails(BaseDetails); 20 dgvBaseDetails.DataSource = dt_BaseDetails.DefaultView; 21 } 22 23 private void btnAdd_Click(object sender, EventArgs e) 24 { 25 //给指定的BaseName添加具体的BaseDetails 26 if (lstBaseName.SelectedIndex != -1) 27 { 28 ListBoxItemObject lstItemObj = (ListBoxItemObject)lstBaseName.SelectedItem; 29 frm_BaseAdd frmBaseAdd = new frm_BaseAdd(int.Parse(lstItemObj.id), lstItemObj.name); 30 frmBaseAdd.refreshEvent += new frm_BaseAdd.dgvRefresh(dgvBaseDetails_Blind); 31 frmBaseAdd.ShowDialog(); 32 } 33 else 34 { 35 MessageBox.Show("请选中要添加的基本资料", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error); 36 } 37 } 38 //........省略 39 } 40 }
frmSun
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Base 11 { 12 public partial class frm_BaseAdd : Form 13 { 14 private int BaseNameID; 15 public delegate void dgvRefresh(int BaseNameID); 16 public event dgvRefresh refreshEvent; 17 18 public frm_BaseAdd() 19 { 20 InitializeComponent(); 21 } 22 23 24 public frm_BaseAdd(int BaseNameID,string BaseName) 25 { 26 //重截构造函数传递参数 27 InitializeComponent(); 28 this.BaseNameID = BaseNameID; 29 this.Text = "添加"+BaseName+"基础资料"; 30 grpTitle.Text = BaseName+"基础资料设置"; 31 } 32 33 private void btnCancel_Click(object sender, EventArgs e) 34 { 35 this.Close(); 36 } 37 38 private void btnSave_Click(object sender, EventArgs e) 39 { 40 Save(); 41 } 42 43 44 private void Save() 45 { 46 47 //保存基础资料具体内容到 48 string BaseDetails = txtBaseDetails.Text; 49 bool IsDefault = chkIsDefault.Checked; 50 BLL_Base.frm_Base.InsertBaseDetails(BaseNameID, BaseDetails, IsDefault); 51 MessageBox.Show("添加成功", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Information); 52 txtBaseDetails.Text = ""; 53 chkIsDefault.Checked = false; 54 refreshEvent(BaseNameID); 55 } 56 57 58 } 59 }
讲解:
本例子要实现在frmFather中打开frmSun,当frmSun保存时调用frmFather中的dgvBaseDetails_Blind(int BaseDetails)方法来刷新DataGridView.
1、在父窗体中,创建子窗体时,绑定方法到委托,见frmFather中的30行代码:
frmBaseAdd.refreshEvent += new frm_BaseAdd.dgvRefresh(dgvBaseDetails_Blind);
frmBaseAdd 这个是frmSun窗体名,refreshEvent是frmSun中定义的委托对象,frm_BaseAdd.dgvRefresh()是frmSun中定义的委托。见frmSun代码的15、16行。
2、在子窗体中,定义委托类型和创建委托对象。
public delegate void dgvRefresh(int BaseNameID);
public event dgvRefresh refreshEvent;
public delegate:表示是委托,公用
void:无返回值
dgvRefresh(int BaseNameID):委托方法,参数是一个整形
public event:http://msdn.microsoft.com/zh-cn/library/8627sbea.aspx EVENT参考这里
dgvRefresh refreshEvent 创建委托事件refreshEvent
3、在子窗体中,调用refreshEvent,来委托调用父窗体的dgvBaseDetails_Blind,见frmSun中的54行
refreshEvent(BaseNameID);
个人总结:
第二种方法: 用变量,Form4和Form5是同一组 Form4
View Code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace TestMouseMove { public partial class Form4 : Form { public Form4() { InitializeComponent(); }
#region 子窗口刷新父窗口的值
private string strLabel1 = "";
public string StrLabel1 { get { return strLabel1; } set { strLabel1 = value; this.label1.Text = strLabel1; } } #endregion
private void button1_Click(object sender, EventArgs e) { Form5 form5 = new Form5(this);//这里注意传个this form5.Show(); } } }
Form5
View Code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace TestMouseMove { public partial class Form5 : Form { Form4 form4 = new Form4();
public Form5(Form4 formFrm)//这个构造方法里有参数 { form4 = formFrm; //这个必须要有 InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { form4.StrLabel1 = this.textBox1.Text; } } }
|
请发表评论