许昌公务员网络培训学院,里番吧,郭彤彤
在winfom中可以在页面上多个按钮或者右键的点击事件中触发同一个自定义的委托事件。
在位置一按钮点击事件中触发
string parentpath = system.io.directory.getparent("指定路径").tostring(); //获取指定路径的父级目录并作为参数调用工具类的方法 datatreelisthelper.taskview(parentpath);
在位置二右键点击触发
将自定义右键的方法定义在上面的工具类中,在工具类中直接调用触发的方法
system.windows.forms.menuitem mnutaskview = new system.windows.forms.menuitem(); mnutaskview.text = "查看任务"; mnutaskview.click += delegate(object s, eventargs ea) { string parentpath = directory.getparent(stridvalue).tostring(); taskview(parentpath); };
在工具类中的触发的方法中
public static void taskview(string currentpath) { //判断当前路径下是否有任务文件 list<string> taskfilelist = filehelper.getfilelistwithextend(new directoryinfo(currentpath), "*.pcj"); if(taskfilelist == null || taskfilelist.count ==0) { xtramessagebox.show("当前路径下没有任务文件"); } else if (taskfilelist.count > 1) { xtramessagebox.show("当前路径下含有多个任务文件"); } else { frmtaskview taskview = new dialog.frmtaskview(); taskview.show(); //触发查看任务事件 triggertaskview(taskfilelist[0]); }
进行逻辑的判断和触发
在触发器中触发事件
public static void triggertaskview(string taskpath) { if (ontaskview != null) { ontaskview(taskpath); } }
在当前工具类中自顶义委托和事件
public delegate void taskviewdelegete(string taskpath); public static event taskviewdelegete ontaskview;
再要执行事件的窗体的构造方法中进行事件的订阅
public frmtaskview() { initializecomponent(); datatreelisthelper.ontaskview -= datatreelisthelper_ontaskview; datatreelisthelper.ontaskview += datatreelisthelper_ontaskview; }
编写具体实现的业务逻辑
private void datatreelisthelper_ontaskview(string taskpath) { if (taskpath != null) { this.taskusercontrol1.initialtaskusercontrol(taskpath); } }
为了以防事件没法解除订阅,在窗口关闭事件中进行事件的取消订阅
private void frmtaskview_formclosing(object sender, formclosingeventargs e) { datatreelisthelper.ontaskview -= datatreelisthelper_ontaskview; }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
浅谈Visual Studio 2019 Vue项目的目录结构
Visual Studio 2019配置vue项目的图文教程详解
网友评论