Child form ff = new child form
| The code is as follows: |
Copy code |
Ff. showdialog (); If (subform. DialogResult = DialogResult. OK) { Datashow (); } |
This method can be written in the button_click event of the parent form.
Another method is to use the asp.net delegate (through simulation, ASP. NET can execute code and access resources in an authenticated and authorized user context, but can only run ASP.. NET server. To access resources on other computers on behalf of a simulated user, you need an authentication delegate (delegated for short ). You can think of delegation as a more powerful form of simulation because it enables simulation through the network. Let's look at an instance.
Let's take a look at the simple delegation
| The code is as follows: |
Copy 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 delegate void SetVisiableHandler (); Public partial class Form2: Form { Public Form2 () { InitializeComponent (); } Private void button#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
| The code is as follows: |
Copy 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 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 (); } } } } |
About delegation
1. The namespace defines a delegate void CustomEventHandler ()
2. The user control defines the event public event CustomEventHandler CutomEvent (), and then calls CutomEvent () where appropriate ();
3. Someone else registers his method on the page: UserControlInstance. CustomEvent (CustomMethod); CustomMethod is his own method (re-bind data and so on .), The signature must be consistent with the delegate.
(Note: the registration method must be executed every time pageload is executed)
If you do not understand delegation, refer to http://msdn.microsoft.com/zh-cn/library/aa291350 (v = vs.71). aspx