场景
C#中委托与事件的使用-以Winform中跨窗体传值为例:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100150700
参照上文在定义事件时传递参数为简单的string,如果要传递比较复杂的参数,那么就可以使用对象将参数进行封装。
博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
在窗体A中定义委托与事件
public delegate void RefreshChartInOneDelegete(XYModel xYModel); public static event RefreshChartInOneDelegete OnRefreshChartInOne;
其中XYModel就是自己封装的参数的Model,具体可以根据需要自己封装。
public class XYModel { //存储X轴属性 private XAxisModel xAxisModel; //存储Y轴属性 private List<YAxisModel> yAxisModelList; public XAxisModel XAxisModel { get { return xAxisModel; } set { xAxisModel = value; } } public List<YAxisModel> YAxisModelList { get { return yAxisModelList; } set { yAxisModelList = value; } } }
定义触发器
xYModel.YAxisModelList = yAxisModelList; if (OnRefreshChartInOne != null) { OnRefreshChartInOne(xYModel); }
然后在窗体B中进行事件订阅
FrmChartOptionInOneCurCom.OnRefreshChartInOne += new FrmChartOptionInOneCurCom.RefreshChartInOneDelegete(ChartCompareHelper_OnRefreshChart);
在窗体B中编写具体的实现
private void ChartCompareHelper_OnRefreshChart(XYModel xYModel) { xYModelStore = xYModel; ChartCompareHelper.RefreshPaneComInOne(this.zedGraphControl1,xYModel.YAxisModelList); }