关键词: C#
一. 简介
1. 程序主界面,如图1
图 1 程序主界面
2. 点击主界面的“打开子界面”,会弹出如下子界面,如图2
图 2 打开子界面
3. 点击子界面的确定键,会将子界面设置的参数传回到主界面,如图3
图 3 传递参数
二. 参考代码
新建类
1. /// <summary>
2. /// 创建传递参数的类
3. /// </summary>
4. public class CustomUtility
5. {
6. /// <summary>
7. /// 定义传递的参数
8. /// </summary>
9. public struct ReturnStruct
10. {
11. public string sData1;
12. public string sData2;
13. public string sData3;
14. public ReturnStruct(string s1, string s2, string s3)
15. {
16. sData1 = s1;
17. sData2 = s2;
18. sData3 = s3;
19. }
20. }
21. }
主窗体事件
1. /// <summary>
2. /// 控件点击事件
3. /// </summary>
4. /// <param name="sender"></param>
5. /// <param name="e"></param>
6. private void buttonOpenChild_Click(object sender, EventArgs e)
7. {
8. //实例化
9. CustomUtility.ReturnStruct rsTest = new CustomUtility.ReturnStruct("Data1", "Data2", "Data3");
10. ChildForm childForm = new ChildForm(rsTest);
11.
12. // 在父窗体使用Lambda表达式自定义事件赋给子窗体内定义的事件,通过在子窗体内调用事件实现传参
13. childForm.EventTest += (obj, args) =>
14. {
15. CustomUtility.ReturnStruct rsTemp = childForm.GetReturnStruct();
16. rsTest.sData1 = args.Args.sData1;
17. rsTest.sData2 = args.Args.sData2;
18. rsTest.sData3 = args.Args.sData3;
19. textBoxResult.Text = string.Format("1:{0}rn2:{1}rn3:{2}", rsTest.sData1, rsTest.sData2, rsTest.sData3);
20. };
21.
22. //显示子窗体Form
23. childForm.ShowDialog();
24. }
子窗体
1. public partial class ChildForm : Form
2. {
3. //新建类
4. public class TEventArgs<T> : EventArgs
5. {
6. public T Args
7. {
8. get;
9. private set;
10. }
11.
12. public TEventArgs(T args)
13. {
14. this.Args = args;
15. }
16. }
17.
18. [Browsable(false)]
19. internal event EventHandler<TEventArgs<CustomUtility.ReturnStruct>> EventTest;
20.
21. CustomUtility.ReturnStruct rs;
22. public CustomUtility.ReturnStruct GetReturnStruct()
23. {
24. return rs;
25. }
26.
27. /// <summary>
28. /// 构造函数1:无参数
29. /// </summary>
30. public ChildForm()
31. {
32. InitializeComponent();
33. }
34.
35. /// <summary>
36. /// 构造函数2:有一个ReturnStruct类型的参数
37. /// </summary>
38. /// <param name="r"></param>
39. public ChildForm(CustomUtility.ReturnStruct r)
40. {
41. InitializeComponent();
42. this.rs.sData1 = r.sData1;
43. this.rs.sData2 = r.sData2;
44. this.rs.sData3 = r.sData3;
45. }
46.
47. /// <summary>
48. /// 窗体加载事件
49. /// </summary>
50. /// <param name="sender"></param>
51. /// <param name="e"></param>
52. private void ChildForm_Load(object sender, EventArgs e)
53. {
54. textBox1.Text = rs.sData1;
55. textBox2.Text = rs.sData2;
56. textBox3.Text = rs.sData3;
57. }
58.
59. /// <summary>
60. /// 单击确定会将三个TextBox的值传递到父窗体中
61. /// </summary>
62. /// <param name="sender"></param>
63. /// <param name="e"></param>
64. private void buttonConfirm_Click(object sender, EventArgs e)
65. {
66. // 调用事件EventTest
67. if (this.EventTest != null)
68. {
69. CustomUtility.ReturnStruct rs = new CustomUtility.ReturnStruct(textBox1.Text, textBox2.Text, textBox3.Text);
70. EventTest(this, new TEventArgs<CustomUtility.ReturnStruct>(rs));
71. }
72. this.Close();
73. }
74.
75. /// <summary>
76. /// 取消
77. /// </summary>
78. /// <param name="sender"></param>
79. /// <param name="e"></param>
80. private void buttonCancel_Click(object sender, EventArgs e)
81. {
82. this.Close();
83. }
84. }
Copyright © 2016-2024 JYTEK All Rights Reserved.