用事件注册实现窗口之间传值
事件和数据定义:
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace WindowsApplication1
6{
7 public class MyEventArgs : EventArgs
8 {
9 private string _eventTag;
10
11 public MyEventArgs(string eventTag)
12 {
13 this._eventTag = eventTag;
14 }
15
16 public string EventTag
17 {
18 get
19 {
20 return this._eventTag;
21 }
22 }
23 }
24
25 public class MyData
26 {
27 public delegate void DataChanged(object sender, MyEventArgs e);
28 public event DataChanged DataChangedEvent;
29
30 private string _data;
31
32 public MyData(string data)
33 {
34 this._data = data;
35 }
36
37 public string Data
38 {
39 get
40 {
41 return this._data;
42 }
43 set
44 {
45 this._data = value;
46
47 if (this.DataChangedEvent != null)
48 {
49 MyEventArgs myeventArags = new MyEventArgs(this._data);
50
51 DataChangedEvent(this, myeventArags);
52 }
53 }
54 }
55 }
56}
57
2using System.Collections.Generic;
3using System.Text;
4
5namespace WindowsApplication1
6{
7 public class MyEventArgs : EventArgs
8 {
9 private string _eventTag;
10
11 public MyEventArgs(string eventTag)
12 {
13 this._eventTag = eventTag;
14 }
15
16 public string EventTag
17 {
18 get
19 {
20 return this._eventTag;
21 }
22 }
23 }
24
25 public class MyData
26 {
27 public delegate void DataChanged(object sender, MyEventArgs e);
28 public event DataChanged DataChangedEvent;
29
30 private string _data;
31
32 public MyData(string data)
33 {
34 this._data = data;
35 }
36
37 public string Data
38 {
39 get
40 {
41 return this._data;
42 }
43 set
44 {
45 this._data = value;
46
47 if (this.DataChangedEvent != null)
48 {
49 MyEventArgs myeventArags = new MyEventArgs(this._data);
50
51 DataChangedEvent(this, myeventArags);
52 }
53 }
54 }
55 }
56}
57
父窗体MainForm代码如下:
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8
9namespace WindowsApplication1
10{
11 public partial class MainForm : Form
12 {
13 private MyData _data = new MyData("Init value");
14
15 public MainForm()
16 {
17 InitializeComponent();
18 _data.DataChangedEvent += new MyData.DataChanged(_data_DataChangedEvent);
19 SForm1 f1 = new SForm1();
20 SForm2 f2 = new SForm2();
21
22 f1.Data = _data;
23 f2.Data = _data;
24
25 f1.Show();
26 f2.Show();
27 }
28
29 void _data_DataChangedEvent(object sender, MyEventArgs e)
30 {
31 this.textBox1.Text = e.EventTag;
32 }
33
34 private void button1_Click(object sender, EventArgs e)
35 {
36 this._data.Data = this.textBox1.Text;
37 }
38
39
40 }
41}
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8
9namespace WindowsApplication1
10{
11 public partial class MainForm : Form
12 {
13 private MyData _data = new MyData("Init value");
14
15 public MainForm()
16 {
17 InitializeComponent();
18 _data.DataChangedEvent += new MyData.DataChanged(_data_DataChangedEvent);
19 SForm1 f1 = new SForm1();
20 SForm2 f2 = new SForm2();
21
22 f1.Data = _data;
23 f2.Data = _data;
24
25 f1.Show();
26 f2.Show();
27 }
28
29 void _data_DataChangedEvent(object sender, MyEventArgs e)
30 {
31 this.textBox1.Text = e.EventTag;
32 }
33
34 private void button1_Click(object sender, EventArgs e)
35 {
36 this._data.Data = this.textBox1.Text;
37 }
38
39
40 }
41}
子窗体SForm1代码(SForm2的代码和SForm1的代码相同):
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8
9namespace WindowsApplication1
10{
11 public partial class SForm1 : Form
12 {
13 private MyData _data;
14
15 public SForm1()
16 {
17 InitializeComponent();
18 }
19
20 void _data_DataChangedEvent(object sender, MyEventArgs e)
21 {
22 this.textBox1.Text = e.EventTag;
23 }
24
25 public MyData Data
26 {
27 set
28 {
29 this._data = value;
30 this._data.DataChangedEvent += new MyData.DataChanged(_data_DataChangedEvent);
31 }
32 }
33
34 private void button1_Click(object sender, EventArgs e)
35 {
36 this._data.Data = this.textBox1.Text;
37 }
38 }
39}
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8
9namespace WindowsApplication1
10{
11 public partial class SForm1 : Form
12 {
13 private MyData _data;
14
15 public SForm1()
16 {
17 InitializeComponent();
18 }
19
20 void _data_DataChangedEvent(object sender, MyEventArgs e)
21 {
22 this.textBox1.Text = e.EventTag;
23 }
24
25 public MyData Data
26 {
27 set
28 {
29 this._data = value;
30 this._data.DataChangedEvent += new MyData.DataChanged(_data_DataChangedEvent);
31 }
32 }
33
34 private void button1_Click(object sender, EventArgs e)
35 {
36 this._data.Data = this.textBox1.Text;
37 }
38 }
39}