WinForm中C#委托和事件的使用(回顾)
2007-12-13 11:30
417 查看
闲了很长时间了,最近想把以前的基础东西回顾一下,先从委托开始。
使用委托一般是在有需要的时候,如 一个窗体想直接调用另外一个窗体的方法,这时候用委托是很方便的。
如果有必要的话,可以写一个专门的类,定义委托和事件 。(代码节选自我一个图表项目)
下面的代码,委托和方法里面参数很多,可以忽略,我也懒得改了。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections.Generic;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Drawing;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace ChartMaker
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
class ChartEvent//FrmReportMain的事件操作类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public delegate void SendChart(int width, int height, int x,int y,Bitmap bitmap, string chartType, long reportId,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string titleInfo, string legendInfo, string backColor, string typeProperty, string dataSetInfo,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string xAxisInfo, string yAxisInfo);//送新建报表
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static event SendChart SendChartEvent;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void doSendChart(int width, int height, int x, int y, Bitmap bitmap, string chartType, long reportId,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string titleInfo, string legendInfo, string backColor, string typeProperty, string dataSetInfo,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string xAxisInfo, string yAxisInfo)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SendChartEvent(width, height, x,y,bitmap, chartType, reportId, titleInfo, legendInfo,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
backColor, typeProperty, dataSetInfo, xAxisInfo, yAxisInfo);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
在WinForm或类的构造函数里面添加:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ChartEvent.SendChartEvent += new ChartEvent.SendChart(AddChart);
需要写个与委托相同具有的 行为、参数、返回值的方法,我这里是AddChart
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void AddChart(int width, int height, int x,int y,Bitmap bitmap, string chartType, long reportId,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string titleInfo, string legendInfo, string backColor, string typeProperty, string dataSetInfo,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string xAxisInfo, string yAxisInfo)//添加图表
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//具体代码省略
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
然后当然是使用方法,一般都在WinForm里面使用。(这里使用的是前面类里面的方法)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ChartEvent.doSendChart(int.Parse(textBox1.Text.Trim()), int.Parse(textBox6.Text.Trim()), ChartMaker.x, ChartMaker.y,bitmap, ChartType, ReportId,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
TitleInfo, LegendInfo, BackColor, TypeProperty, DataSetInfo, XAxisInfo, YAxisInfo);
附带MSDN里面关于委托的解释:
委托是一种引用方法的类型。一旦为委托分配了方法,委托将与该方法具有完全相同的行为。委托方法的使用可以像其他任何方法一样,具有参数和返回值,如下面的示例所示:
public delegate int PerformCalculation(int x, int y);
与委托的签名(由返回类型和参数组成)匹配的任何方法都可以分配给该委托。这样就可以通过编程方式来更改方法调用,还可以向现有类中插入新代码。只要知道委托的签名,便可以分配自己的委托方法。
将方法作为参数进行引用的能力使委托成为定义回调方法的理想选择。例如,可以向排序算法传递对比较两个对象的方法的引用。分离比较代码使得可以采用更通用的方式编写算法。
委托概述
委托具有以下特点:
委托类似于 C++ 函数指针,但它是类型安全的。
委托允许将方法作为参数进行传递。
委托可用于定义回调方法。
委托可以链接在一起;例如,可以对一个事件调用多个方法。
方法不需要与委托签名精确匹配。有关更多信息,请参见协变和逆变。
C# 2.0 版引入了匿名方法的概念,此类方法允许将代码块作为参数传递,以代替单独定义的方法。
使用委托一般是在有需要的时候,如 一个窗体想直接调用另外一个窗体的方法,这时候用委托是很方便的。
如果有必要的话,可以写一个专门的类,定义委托和事件 。(代码节选自我一个图表项目)
下面的代码,委托和方法里面参数很多,可以忽略,我也懒得改了。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections.Generic;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Drawing;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace ChartMaker
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
class ChartEvent//FrmReportMain的事件操作类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public delegate void SendChart(int width, int height, int x,int y,Bitmap bitmap, string chartType, long reportId,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string titleInfo, string legendInfo, string backColor, string typeProperty, string dataSetInfo,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string xAxisInfo, string yAxisInfo);//送新建报表
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static event SendChart SendChartEvent;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void doSendChart(int width, int height, int x, int y, Bitmap bitmap, string chartType, long reportId,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string titleInfo, string legendInfo, string backColor, string typeProperty, string dataSetInfo,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string xAxisInfo, string yAxisInfo)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SendChartEvent(width, height, x,y,bitmap, chartType, reportId, titleInfo, legendInfo,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
backColor, typeProperty, dataSetInfo, xAxisInfo, yAxisInfo);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
在WinForm或类的构造函数里面添加:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ChartEvent.SendChartEvent += new ChartEvent.SendChart(AddChart);
需要写个与委托相同具有的 行为、参数、返回值的方法,我这里是AddChart
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void AddChart(int width, int height, int x,int y,Bitmap bitmap, string chartType, long reportId,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string titleInfo, string legendInfo, string backColor, string typeProperty, string dataSetInfo,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string xAxisInfo, string yAxisInfo)//添加图表
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//具体代码省略
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
然后当然是使用方法,一般都在WinForm里面使用。(这里使用的是前面类里面的方法)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ChartEvent.doSendChart(int.Parse(textBox1.Text.Trim()), int.Parse(textBox6.Text.Trim()), ChartMaker.x, ChartMaker.y,bitmap, ChartType, ReportId,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
TitleInfo, LegendInfo, BackColor, TypeProperty, DataSetInfo, XAxisInfo, YAxisInfo);
附带MSDN里面关于委托的解释:
委托是一种引用方法的类型。一旦为委托分配了方法,委托将与该方法具有完全相同的行为。委托方法的使用可以像其他任何方法一样,具有参数和返回值,如下面的示例所示:
public delegate int PerformCalculation(int x, int y);
与委托的签名(由返回类型和参数组成)匹配的任何方法都可以分配给该委托。这样就可以通过编程方式来更改方法调用,还可以向现有类中插入新代码。只要知道委托的签名,便可以分配自己的委托方法。
将方法作为参数进行引用的能力使委托成为定义回调方法的理想选择。例如,可以向排序算法传递对比较两个对象的方法的引用。分离比较代码使得可以采用更通用的方式编写算法。
委托概述
委托具有以下特点:
委托类似于 C++ 函数指针,但它是类型安全的。
委托允许将方法作为参数进行传递。
委托可用于定义回调方法。
委托可以链接在一起;例如,可以对一个事件调用多个方法。
方法不需要与委托签名精确匹配。有关更多信息,请参见协变和逆变。
C# 2.0 版引入了匿名方法的概念,此类方法允许将代码块作为参数传递,以代替单独定义的方法。
相关文章推荐
- WinForm 用户控件使用——设置用户控件的按钮事件
- c# winform动态生成窗体及控件,并使用控件事件
- C# winform窗体间传值(使用委托或事件)
- winform使用委托和事件完成两个窗体之间的通信实例
- C#委托和事件的使用实例
- 观察者设计模式(C#委托和事件的使用)
- C#委托事件的使用
- C#委托-在窗体中使用委托和事件
- WINCE系统中WINFORM的按键事件的使用方法简介
- C#使用事件方式Winform窗体之间传值
- c#委托和事件的简单使用,简单介绍observer设计模式
- WinForm 使用TreeView控件作为导航的 TreeNode(TreeView)点击事件(Click、DoubleClick ~)的一般做法
- winform 使用快捷键来触发按钮事件
- C#委托事件使用简单示例
- C# winform窗体间传值(使用委托或事件)
- C#委托与事件的简单使用
- winform使用委托和事件来完成两个窗体之间通信的实例
- [置顶] 如何统计网站访问量,代码实现使用全局变量 Application,start,end,Session_start,Session_end事件
- C#委托,事件理解入门
- C# 委托/事件在Unit 4000 y中的使用(续)