This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17570|Reply: 1

[WinForm] Delegate values between Winform forms

[Copy link]
Posted on 12/28/2015 12:27:20 PM | | | |
The first form (which is the main form):

  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. namespace winformDelegate
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }

  17.         private void button1_Click(object sender, EventArgs e)
  18.         {
  19.            Form2 frm=new Form2();
  20.            frm.Myevent += GetValue;
  21.             frm.Show();
  22.         }

  23.         public void GetValue(string text)
  24.         {
  25.             textBox1.Text = text;
  26.         }
  27.     }
  28. }
Copy code


Second form:
  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. namespace winformDelegate
  10. {
  11.     public partial class Form2 : Form
  12.     {
  13.         public delegate void DelegaeValue
  14.             (string text);

  15.         public event DelegaeValue Myevent;

  16.         protected virtual void ValueInformation(string text)
  17.         {
  18.             DelegaeValue handler = Myevent;
  19.             if (handler != null)
  20.                 handler(text);
  21.         }

  22.         public Form2()
  23.         {
  24.             InitializeComponent();
  25.         }

  26.         private void button1_Click(object sender, EventArgs e)
  27.         {
  28.             if (Myevent != null)
  29.             {
  30.             }
  31.             ValueInformation(textBox1.Text);
  32.         }


  33.     }
  34. }
Copy code






Previous:onsubmit event for form - the best way to implement validation before form submission
Next:JSONP - Solving the problem of cross-domain access in Ajax
 Landlord| Posted on 8/17/2017 11:28:12 AM |


You can pass value without delegation

Form 1 code:



Form 2 code:



Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com