C#利用委托实现窗体间的值传递 .

A、网上有很多方法,大家可搜一下,都可用。

B、这里只是说明下是只利用委托,学习基本的委托运用。

方法如下:

1、C#建立一个默认工程,默认窗体Form1

2、加入一个新窗体,默认Form2

3、Form1窗体上放一个Label,一个Button,属性分别为:

this.label1.Text = ""; // 清空label的值

this.button1.Text="otherForm"; //设置button显示为otherForm

  1. protectedoverridevoidOnLoad(EventArgse)
  2. {
  3. base.OnLoad(e);
  4. this.label1.Text="";
  5. this.button1.Text="otherForm";
  6. }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.label1.Text = ""; this.button1.Text = "otherForm"; }

4、Form2窗体上放一个textbox1,一个Button,属性分别为:

this.textbox1.Text = ""; //清空textbox的值

this.button1.Text = "input"; //设置button显示为input(输入)

  1. protectedoverridevoidOnLoad(EventArgse)
  2. {
  3. base.OnLoad(e);
  4. this.textBox1.Text="";
  5. this.button1.Text="input";
  6. }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.textBox1.Text = ""; this.button1.Text = "input"; }

5、上面的步骤就建立了两个基本的窗体,接下来回到Form1的代码设计视图

拷贝下面代码

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.ComponentModel;
  4. usingSystem.Data;
  5. usingSystem.Drawing;
  6. usingSystem.Linq;
  7. usingSystem.Text;
  8. usingSystem.Windows.Forms;
  9. namespaceWindowsFormsApplication1
  10. {
  11. publicpartialclassForm1:Form
  12. {
  13. publicForm1()
  14. {
  15. InitializeComponent();
  16. }
  17. protectedoverridevoidOnLoad(EventArgse)
  18. {
  19. base.OnLoad(e);
  20. this.label1.Text="";
  21. this.button1.Text="otherForm";
  22. }
  23. privatevoidbutton1_Click(objectsender,EventArgse)
  24. {
  25. Form2frm2=newForm2();
  26. returnValueEvent+=newreturnValue(Form1_returnValueEvent);
  27. frm2.Show();
  28. }
  29. voidForm1_returnValueEvent(stringstr)
  30. {
  31. //thrownewNotImplementedException();
  32. this.label1.Text=str;
  33. }
  34. //定义委托
  35. publicdelegatevoidreturnValue(stringstr);
  36. //委托的事件
  37. publicstaticeventreturnValuereturnValueEvent;
  38. //委托的事件的函数
  39. publicvoiddoReturnValue(stringstr)
  40. {
  41. returnValueEvent(str);
  42. }
  43. }
  44. }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.label1.Text = ""; this.button1.Text = "otherForm"; } private void button1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); returnValueEvent += new returnValue(Form1_returnValueEvent); frm2.Show(); } void Form1_returnValueEvent(string str) { // throw new NotImplementedException(); this.label1.Text = str; } // 定义委托 public delegate void returnValue(string str); // 委托的事件 public static event returnValue returnValueEvent; // 委托的事件的函数 public void doReturnValue(string str) { returnValueEvent(str); } } }

6、在Form2的代码设计视图中,拷贝下面代码

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.ComponentModel;
  4. usingSystem.Data;
  5. usingSystem.Drawing;
  6. usingSystem.Linq;
  7. usingSystem.Text;
  8. usingSystem.Windows.Forms;
  9. namespaceWindowsFormsApplication1
  10. {
  11. publicpartialclassForm2:Form
  12. {
  13. publicForm2()
  14. {
  15. InitializeComponent();
  16. }
  17. protectedoverridevoidOnLoad(EventArgse)
  18. {
  19. base.OnLoad(e);
  20. this.textBox1.Text="";
  21. this.button1.Text="input";
  22. }
  23. privatevoidbutton1_Click(objectsender,EventArgse)
  24. {
  25. Form1frm1=newForm1();
  26. frm1.doReturnValue(this.textBox1.Text);
  27. }
  28. }
  29. }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.textBox1.Text = ""; this.button1.Text = "input"; } private void button1_Click(object sender, EventArgs e) { Form1 frm1 = new Form1(); frm1.doReturnValue(this.textBox1.Text); } } }

7、运行后,就可以在两个窗体间传递参数了

注:重点是第六步中的,对委托的定义和调用

returnValueEvent += new returnValue(Form1_returnValueEvent);

void Form1_returnValueEvent(string str)
{
// throw new NotImplementedException();
this.label1.Text = str;
}

// 定义委托
public delegate void returnValue(string str);
// 委托的事件
public static event returnValue returnValueEvent;
// 委托的事件的函数
public void doReturnValue(string str)
{
returnValueEvent(str);
}

在Form2中的调用,在按钮的click事件中

Form1 frm1 = new Form1();
frm1.doReturnValue(this.textBox1.Text);

运行效果图如下:

C#利用委托实现窗体间的值传递 .

A、网上有很多方法,大家可搜一下,都可用。

B、这里只是说明下是只利用委托,学习基本的委托运用。

方法如下:

1、C#建立一个默认工程,默认窗体Form1

2、加入一个新窗体,默认Form2

3、Form1窗体上放一个Label,一个Button,属性分别为:

this.label1.Text = ""; // 清空label的值

this.button1.Text="otherForm"; //设置button显示为otherForm

  1. protectedoverridevoidOnLoad(EventArgse)
  2. {
  3. base.OnLoad(e);
  4. this.label1.Text="";
  5. this.button1.Text="otherForm";
  6. }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.label1.Text = ""; this.button1.Text = "otherForm"; }

4、Form2窗体上放一个textbox1,一个Button,属性分别为:

this.textbox1.Text = ""; //清空textbox的值

this.button1.Text = "input"; //设置button显示为input(输入)

  1. protectedoverridevoidOnLoad(EventArgse)
  2. {
  3. base.OnLoad(e);
  4. this.textBox1.Text="";
  5. this.button1.Text="input";
  6. }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.textBox1.Text = ""; this.button1.Text = "input"; }

5、上面的步骤就建立了两个基本的窗体,接下来回到Form1的代码设计视图

拷贝下面代码

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.ComponentModel;
  4. usingSystem.Data;
  5. usingSystem.Drawing;
  6. usingSystem.Linq;
  7. usingSystem.Text;
  8. usingSystem.Windows.Forms;
  9. namespaceWindowsFormsApplication1
  10. {
  11. publicpartialclassForm1:Form
  12. {
  13. publicForm1()
  14. {
  15. InitializeComponent();
  16. }
  17. protectedoverridevoidOnLoad(EventArgse)
  18. {
  19. base.OnLoad(e);
  20. this.label1.Text="";
  21. this.button1.Text="otherForm";
  22. }
  23. privatevoidbutton1_Click(objectsender,EventArgse)
  24. {
  25. Form2frm2=newForm2();
  26. returnValueEvent+=newreturnValue(Form1_returnValueEvent);
  27. frm2.Show();
  28. }
  29. voidForm1_returnValueEvent(stringstr)
  30. {
  31. //thrownewNotImplementedException();
  32. this.label1.Text=str;
  33. }
  34. //定义委托
  35. publicdelegatevoidreturnValue(stringstr);
  36. //委托的事件
  37. publicstaticeventreturnValuereturnValueEvent;
  38. //委托的事件的函数
  39. publicvoiddoReturnValue(stringstr)
  40. {
  41. returnValueEvent(str);
  42. }
  43. }
  44. }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.label1.Text = ""; this.button1.Text = "otherForm"; } private void button1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); returnValueEvent += new returnValue(Form1_returnValueEvent); frm2.Show(); } void Form1_returnValueEvent(string str) { // throw new NotImplementedException(); this.label1.Text = str; } // 定义委托 public delegate void returnValue(string str); // 委托的事件 public static event returnValue returnValueEvent; // 委托的事件的函数 public void doReturnValue(string str) { returnValueEvent(str); } } }

6、在Form2的代码设计视图中,拷贝下面代码

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.ComponentModel;
  4. usingSystem.Data;
  5. usingSystem.Drawing;
  6. usingSystem.Linq;
  7. usingSystem.Text;
  8. usingSystem.Windows.Forms;
  9. namespaceWindowsFormsApplication1
  10. {
  11. publicpartialclassForm2:Form
  12. {
  13. publicForm2()
  14. {
  15. InitializeComponent();
  16. }
  17. protectedoverridevoidOnLoad(EventArgse)
  18. {
  19. base.OnLoad(e);
  20. this.textBox1.Text="";
  21. this.button1.Text="input";
  22. }
  23. privatevoidbutton1_Click(objectsender,EventArgse)
  24. {
  25. Form1frm1=newForm1();
  26. frm1.doReturnValue(this.textBox1.Text);
  27. }
  28. }
  29. }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.textBox1.Text = ""; this.button1.Text = "input"; } private void button1_Click(object sender, EventArgs e) { Form1 frm1 = new Form1(); frm1.doReturnValue(this.textBox1.Text); } } }

7、运行后,就可以在两个窗体间传递参数了

注:重点是第六步中的,对委托的定义和调用

returnValueEvent += new returnValue(Form1_returnValueEvent);

void Form1_returnValueEvent(string str)
{
// throw new NotImplementedException();
this.label1.Text = str;
}

// 定义委托
public delegate void returnValue(string str);
// 委托的事件
public static event returnValue returnValueEvent;
// 委托的事件的函数
public void doReturnValue(string str)
{
returnValueEvent(str);
}

在Form2中的调用,在按钮的click事件中

Form1 frm1 = new Form1();
frm1.doReturnValue(this.textBox1.Text);

运行效果图如下:

C#利用委托实现窗体间的值传递 .