Use a delegate to implement child forms to operate controls in the parent form

Source: Internet
Author: User

CodeImplementation
In form1, A lable1; In form2, control the display or hiding of lable1 in form1!
Form1 code:

View plaincopy to clipboardprint?

  1. Namespace windowsapplication2
  2. {
  3. Public Delegate void setvisiablehandler ();
  4. Public partial class form1: Form
  5. {
  6. Public form1 ()
  7. {
  8. Initializecomponent ();
  9. }
  10. Private void button#click (Object sender, eventargs E)
  11. {
  12. Form2 FRM = new form2 (New setvisiablehandler (setvisiable ));
  13. FRM. Show ();
  14. }
  15. Private void setvisiable ()
  16. {
  17. Setvisiable (this. label1 ,! This. label1.visible );
  18. }
  19. Private void setvisiable (Control, bool visiable)
  20. {
  21. If (this. Controls. Contains (Control ))
  22. {
  23. Control. Visible = visiable;
  24. }
  25. }
  26. }
  27. }

Namespace windowsapplication2 {Public Delegate void setvisiablehandler (); Public partial class form1: FORM {public form1 () {initializecomponent ();} private void button1_click (Object sender, eventargs E) {form2 FRM = new form2 (New setvisiablehandler (setvisiable); frm. show ();} private void setvisiable () {setvisiable (this. label1 ,! This. label1.visible);} private void setvisiable (Control, bool visiable) {If (this. Controls. Contains (Control) {control. Visible = visiable ;}}}}

Form2 code

View plaincopy to clipboardprint?

  1. Namespace windowsapplication2
  2. {
  3. Public partial class form2: FORM {
  4. Private setvisiablehandler m_setvisible;
  5. Public form2 (setvisiablehandler setvisible)
  6. {
  7. Initializecomponent ();
  8. This. m_setvisible = setvisible;
  9. }
  10. Private void btnvisible_click (Object sender, eventargs E)
  11. {
  12. If (this. m_setvisible! = NULL)
  13. {
  14. This. m_setvisible ();
  15. }
  16. }
  17. }
  18. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.