当前位置: 移动技术网 > IT编程>开发语言>c# > C#中在定义事件委托时怎样跨窗体传递参数

C#中在定义事件委托时怎样跨窗体传递参数

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

场景

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

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

参照上文在定义事件时传递参数为简单的string,如果要传递比较复杂的参数,那么就可以使用对象将参数进行封装。

博客主页:

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

实现

在窗体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);
        }

 

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

相关文章:

验证码:
移动技术网