子表單 ff=new 子表單
| 代碼如下 |
複製代碼 |
ff.showdialog(); if (子表單.DialogResult==DialogResult.Ok) { datashow(); } |
這個方法寫在父表單的button_click事件中就可以啦
還有一種方法就是利用了asp.net中的委託(通過類比,ASP.NET 能夠在一個經過身分識別驗證和授權的使用者上下文中執行代碼和訪問資源,但是只能在運行 ASP.NET 的伺服器上進行。若要代表所類比的使用者訪問其他電腦上的資源,需要身分識別驗證委託(簡稱委託)。可以將委託看作是一種更強大的類比形式,因為它通過網路啟用類比)來做了。下面我們來看個執行個體
先來看看委託簡單實
| 代碼如下 |
複製代碼 |
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 delegate void SetVisiableHandler(); public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form3 frm = new Form3(new SetVisiableHandler(SetVisiable)); frm.Show(); } private void SetVisiable() { SetVisiable(this.label1, !this.label1.Visible); } private void SetVisiable(Control control, bool visiable) { if (this.Controls.Contains(control)) { control.Visible = visiable; } } } } |
form3
| 代碼如下 |
複製代碼 |
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 Form3 : Form { private SetVisiableHandler m_setVisible; public Form3(SetVisiableHandler setvisible) { InitializeComponent(); this.m_setVisible = setvisible; } private void btnVisible_Click(object sender, EventArgs e) { if (this.m_setVisible != null) { this.m_setVisible(); } } } } |
關於委託
1.命名空間定義一個委託 delegate void CustomEventHandler()
2.使用者控制項 定義事件 public event CustomEventHandler CutomEvent(); 然後再合適的地方調用 CutomEvent();
3.別人在頁面裡註冊他的方法: UserControlInstance.CustomEvent(CustomMethod); CustomMethod為他自己的方法(重新綁定資料之類。),簽名要和委託一致
(註:註冊方法每次pageload 都要執行)
如果你不瞭解委託可以參考 http://msdn.microsoft.com/zh-cn/library/aa291350(v=vs.71).aspx