image.pngimage.png

    Form1:

    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.Threading.Tasks;
    9. using System.Windows.Forms;
    10. namespace _147_窗体传值
    11. {
    12. public partial class Form1 : Form
    13. {
    14. public Form1()
    15. {
    16. InitializeComponent();
    17. }
    18. private void button1_Click(object sender, EventArgs e)
    19. {
    20. Form2 form2 = new Form2(ShowMsg);
    21. form2.Show();
    22. }
    23. void ShowMsg(string str)
    24. {
    25. label1.Text = str;
    26. }
    27. }
    28. }

    From2:

    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.Threading.Tasks;
    9. using System.Windows.Forms;
    10. namespace _147_窗体传值
    11. {
    12. //声明委托
    13. public delegate void DelTest(string str);
    14. public partial class Form2 : Form
    15. {
    16. //声明一个字段
    17. private DelTest _del;
    18. public Form2(DelTest del)
    19. {
    20. this._del = del;
    21. InitializeComponent();
    22. }
    23. private void button1_Click(object sender, EventArgs e)
    24. {
    25. _del(textBox1.Text);
    26. }
    27. }
    28. }