C#学习笔记
对象间数据的传递和回调
以windowsForm程序为例
主窗口向从窗口传递数据
运行示例
设置一个公有属性或设置公有方法来实现
主窗体代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41using 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 MainToOther
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private FormOther otherForm;
private void FormMain_Load(object sender, EventArgs e)
{
otherForm = new FormOther();
otherForm.Show();
}
private void btnSend_Click(object sender, EventArgs e)
{
string userInput = textUserInput.Text.Trim();
if(string.IsNullOrEmpty(userInput))
{
return;
}
else
{
//otherForm.Messige = userInput;
otherForm.ReceiveMessige(userInput);
}
}
}
}
从窗体代码
1 | using System; |
从窗体向主窗体传送数据
运行示例
方法一
示例代码
1 | using System; |
从窗体1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41using 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 OtherToMain
{
public partial class FormOther : Form
{
public FormOther()
{
InitializeComponent();
}
//通过公有属性传递数据
public string UserInput
{
get { return textBox1.Text; }
}
private void button1_Click(object sender, EventArgs e)
{
设置本窗体关闭的原因,以供主窗体查询
this.DialogResult = DialogResult.OK;
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
设置本窗体关闭的原因,以供主窗体查询
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
}
方法二:使用回调实现
主窗体1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35using 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 OtherToMain
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FormOther OtherForm = new FormOther(this);
OtherForm.Show();
}
//从窗体主动的汇报数据,Report方法供从窗体调用
public void Report(string Info)
{
label1.Text = Info;
}
}
}
从窗体
1 | using System; |
一个关于回调的示例
运行示例
实现代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CallBackExamlple
{
//公有接口
public interface ICallBack
{
void run();
}
class CallBackClass : ICallBack
{
public void run()
{
//输出当前时间
Console.WriteLine(DateTime.Now);
}
}
class CallBackClass2 : ICallBack
{
private int counter = 0;
public void run()
{
counter++;
System.Media.SystemSounds.Asterisk.Play();
Console.WriteLine("I am invoked " + counter.ToString() + " times");
}
}
class Controller
{
//定义一个对象集合
private List<ICallBack> CallBackObjects = new List<ICallBack>(); //回调对象
public void AddCallBack(ICallBack callBack)
{
CallBackObjects.Add(callBack);
}
public void Begin()
{
Console.WriteLine("敲任意键回调方法,按ESC键退出...");
while (Console.ReadKey(true).Key != ConsoleKey.Escape)
{
Console.WriteLine();
foreach(ICallBack obj in CallBackObjects)
{
obj.run();
}
}
}
}
class Program
{
static void Main(string[] args)
{
//创建控制器对象,将提供给它的回调对象传入
Controller controller = new Controller();
controller.AddCallBack(new CallBackClass());
controller.AddCallBack(new CallBackClass2());
//如需扩充程序,则只需要做如下操作
//controller.AddCallBack(new MyCallBackCla())
//启动控制器对象运行
controller.Begin();
}
}
}
综合示例
运行示例
主窗体代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45using 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 TwoWayCommunicationInForms
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private FormOther OtherForm;
private void FormMain_Load(object sender, EventArgs e)
{
OtherForm = new FormOther(this);
OtherForm.Show();
}
//将numericUpDown1中数据传送到从窗口中
public void Swap()
{
OtherForm.Trabsnit = (int)numericUpDown1.Value;
}
//接收FromOther中传回的数据
public void Repter(int Info)
{
numericUpDown1.Value = Info;
}
//当numericUpDown1中数字发生改变时执行Swap
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
Swap();
}
}
}
从窗体
1 | using System; |
对象广播,多个对象间信息传递
使用对象集合实现消息广播
运行示例
主窗体代码
1 | using System; |
从窗体代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25using 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 UseObjectCollection
{
public partial class FormOther : Form
{
public FormOther()
{
InitializeComponent();
}
//供主窗体回调,显示主窗体传入的计数器值
public void ShowCounter(int counter)
{
label1.Text = counter.ToString();
}
}
}
利用委托实现消息广播
一个委托变量可以包容一个方法引用列表,能包容多种方法。
调用一次委托变量,所有方法顺序执行
主窗体代码
1 | using System; |
从窗体代码相同如上
利用事件实现消息广播
主窗体代码
1 | using System; |
从窗体代码相同如上
一个对象监控多个对象
运行示例
利用对象引用回调
基本思想是从窗体定义一个MainForm字段,它引用主窗体对象
当从窗体点击按钮时,它通过对象引用调用主窗体的方法向其主动汇报情况
主窗体代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35using 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 ButtonCounterForMoreForm
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
//从窗体点击计数器
private int counter = 0;
//显示结果
public void ShowCounter()
{
counter++;
label2.Text = counter.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
FormOther fm = new FormOther(this);
fm.Show();
}
}
}
从窗体
1 | using System; |
利用委托回调
从窗体定义有一个委托类型的字段CallBackMethod
主窗体对象在创先从窗体对象时,把它自己的公有方法ShowCounter连接到此字段上
当从窗体点击按钮时,它通过委托回调主窗体的方法
主窗体代码将按钮部分改为如下代码
1 | private void button1_Click(object sender, EventArgs e) |
从窗体代码
1 | using System; |
利用自定义事件
从窗体定义了一个MyClick事件
主窗体的RespondTo方法响应这个事件,在事件响应代码中累加计数并更新显示
主窗体
1 | using System; |
从窗体1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35using 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 ButtonCounterForMoreForm2
{
//定义事件委托
public delegate void MyClickDelegate();
public partial class FormOther : Form
{
public FormOther()
{
InitializeComponent();
}
//自定义一个事件
public event MyClickDelegate MyClick;
private void button1_Click(object sender, EventArgs e)
{
if (MyClick != null)
{
//激发MyClick事件
MyClick();
}
}
}
}
多对多的信息交换
改多对多为一对多
设置一个专门的数据交换中心来交换数据
例
这部分代码写得比较乱,就不放了。