Sometimes the sub-form operation needs to call the control operation in the parent form in real time, for example, display the output in the sub-form in the text box of the parent form:
Main form:
[CSHARP] View plaincopy
-
- Mainform. CS:
-
-
- PublicPartialClassMainform: Form
-
- {
- PublicMainform ()
-
- {
-
- Initializecomponent ();
-
- }
-
-
- Private VoidButton#click (ObjectSender, eventargs E)
-
- {
- Subform =NewSubform ();
-
-
- // 3. Add changetextval to the delegate event
-
- // Equivalent:
-
- // Subform. changetextval + = changetextval;
- Subform. changetextval + =NewDelegatechangetextval (changetextval );
-
- Subform. showdialog ();
-
- }
-
-
- Public VoidChangetextval (StringTextval)
-
- {
- This. Textbox1.text = textval;
-
- }
-
- }
Subform:
[CSHARP] View plaincopy
-
- Subform. CS:
-
-
- // 1. Define the delegate type
- Public Delegate VoidDelegatechangetextval (StringTextval );
-
-
- PublicPartialClassSubform: Form
-
- {
-
- // 2. Define a delegate event
- Public EventDelegatechangetextval changetextval;
-
-
- PublicSubform ()
-
- {
-
- Initializecomponent ();
-
- }
-
- Private VoidButton#click (ObjectSender, eventargs E)
-
- {
-
- Changemainformtext (This. Textbox1.text );
-
- }
-
- Private VoidChangemainformtext (StringTextval)
-
- {
-
- // 4. Call the delegate event Function
-
- Changetextval (textval );
-
- }
-
- }