当前位置: 移动技术网 > IT编程>开发语言>c# > C#中怎样跨窗体调用事件-从事件订阅实例入手

C#中怎样跨窗体调用事件-从事件订阅实例入手

2019年10月15日  | 移动技术网IT编程  | 我要评论

场景

c#中委托与事件的使用-以winform中跨窗体传值为例:

https://blog.csdn.net/badao_liumang_qizhi/article/details/100150700

参考上面的博客。

需求是在图形选项窗体中刷新主窗体的图。

注:

博客主页:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

首先是图形选项窗体,即发起方。

先要声明刷新事件(此事件声明在图形选项工具类datacharthelper中):

public static event eventhandler onrefreshchart;

然后图形选项窗体的保存按钮事件中要调用刷新事件时需要触发:

if (this.confirm("保存成功") == dialogresult.ok)
                {
                    common.datachart.datacharthelper.triggerrefreshchart();     //触发图形刷新事件
                    this.dispose();
                }

 

此时会触发在图形选项工具类中的triggerrefreshchart(),回到工具类中:

 public static void triggerrefreshchart()
        {
            if (onrefreshchart != null)
            {
                onrefreshchart(null, system.eventargs.empty);
            }
        }

 

此时需要在主页面进行图的刷新。在主页面窗体的窗体加载事件中进行事件订阅:

common.datachart.datacharthelper.onrefreshchart -= datacharthelper_onrefreshchart;
common.datachart.datacharthelper.onrefreshchart += datacharthelper_onrefreshchart;

 

此时就会执行当前主页面的方法datacharthelper_onrefreshchart:

private void datacharthelper_onrefreshchart(object sender, eventargs e)
        {
            datacharthelper.refreshpane(this.zedgraphcontrol1);
        }

 

此方法会执行具体的操作,具体操作是调用工具类中的刷新方法。

具体流程图示

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网