欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

DevExpress的TreeList实现自定义右键菜单打开文件选择对话框

程序员文章站 2022-06-07 12:22:43
场景 DevExpress的TreeList实现节点上添加自定义右键菜单并实现删除节点功能: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102551175 在上面已经实现自定义右键菜单删除树节点,这里要实现自定义右键菜单实现 ......

场景

devexpress的treelist实现节点上添加自定义右键菜单并实现删除节点功能:

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

在上面已经实现自定义右键菜单删除树节点,这里要实现自定义右键菜单实现

右键功能自定义,比如打开文件选择框。

注:

博客主页:

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

实现

首先绑定treelist的鼠标单击事件

treelist.mouseclick -= treelist_mouseclick;
treelist.mouseclick += treelist_mouseclick;

 

然后在绑定的单击事件中

获取treelist,然后获取其数据源并转换为对象list,然后是相关的业务判断。

然后如果是鼠标右键的话,新增右键菜单项。

private static void treelist_mouseclick(object sender, system.windows.forms.mouseeventargs e)
        {
            devexpress.xtratreelist.treelist treelist = sender as devexpress.xtratreelist.treelist;
            if (treelist != null && treelist.selection.count == 1)
            {
                object idvalue = null;
                string stridvalue = string.empty;
                datatreenode nodedata = null;
                list<datatreenode> datasource = treelist.datasource as list<datatreenode>;
                if (datasource != null)
                {
                    idvalue = treelist.selection[0].getvalue("id");
                    stridvalue = idvalue.tostring();
                    nodedata = datasource.where<datatreenode>(p => p.id == stridvalue).firstordefault<datatreenode>();
                    if (nodedata != null)
                    {
                        if (nodedata.nodetype == datatreenodetypes.file)
                        {
                            treelist.optionsselection.enableappearancefocusedrow = true;                                //启用整行选中

                            #region 右键弹出上下文菜单 - 删除数据文件

                            if (e.button == system.windows.forms.mousebuttons.right)
                            {
                                system.windows.forms.contextmenu ctxmenu = new system.windows.forms.contextmenu();
                                system.windows.forms.menuitem mnudelete = new system.windows.forms.menuitem();
                                mnudelete.text = "删除";
                                mnudelete.click += delegate(object s, eventargs ea) {
                                    dialogresult dialogresult = devexpress.xtraeditors.xtramessagebox.show(string.format("确定要删除此实验数据吗[{0}]?\r\n删

除后无法恢复!", nodedata.id), "霸道标题", system.windows.forms.messageboxbuttons.yesno, messageboxicon.question);
                                    if (dialogresult == dialogresult.yes)
                                    {
                                        try
                                        {
                                            string filename = string.empty;
     

                                            #region 删除对应的树节点

                                            devexpress.xtratreelist.nodes.treelistnode selectednode = treelist.findnodebykeyid(nodedata.id);
                                            if (selectednode != null)
                                            {
                                                selectednode.parentnode.nodes.remove(selectednode);
                                            }

                                            #endregion

                                            treelist.optionsselection.enableappearancefocusedrow = false;                                //禁用整行选中
                                        }
                                        catch(exception ex)
                                        {
                                          
                                            devexpress.xtraeditors.xtramessagebox.show("删除实验数据异常:" + ex.message, "霸道标题", messageboxbuttons.ok,

messageboxicon.error);
                                        }
                                    }
                                };
                                ctxmenu.menuitems.add(mnudelete);
                                #endregion

                                #region 右键弹出上下文菜单 - 导入配置文件
                                system.windows.forms.menuitem mnuimport = new system.windows.forms.menuitem();
                                mnuimport.text = "导入配置文件";
                                mnuimport.click += delegate(object s, eventargs ea)
                                {
                                    openfiledialog importopenfiledialog = new openfiledialog();
                                    importopenfiledialog.showdialog();
                                   
                                };
                                ctxmenu.menuitems.add(mnuimport);

                                #endregion

                                #region 右键弹出上下文菜单 - 导出配置文件
                                system.windows.forms.menuitem mnuexport = new system.windows.forms.menuitem();
                                mnuexport.text = "导出配置文件";
                                mnuexport.click += delegate(object s, eventargs ea)
                                {
                                    dialogresult dialogresult = devexpress.xtraeditors.xtramessagebox.show(string.format("导出[{0}]成功!", nodedata.id), "标题

", system.windows.forms.messageboxbuttons.yesno, messageboxicon.question);
                                   
                                };
                                ctxmenu.menuitems.add(mnuexport);

                                #endregion

                                ctxmenu.show(treelist, new system.drawing.point(e.x, e.y));
                            }

                            return;
                        }
                    }
                }
                treelist.optionsselection.enableappearancefocusedrow = false;                                //禁用整行选中
            }
        }

 

效果

DevExpress的TreeList实现自定义右键菜单打开文件选择对话框

 

 

点击导入配置文件后

DevExpress的TreeList实现自定义右键菜单打开文件选择对话框

 

DevExpress的TreeList实现自定义右键菜单打开文件选择对话框