TreeList implementation of devxpress right click menu to open the file selection dialog box

scene

The TreeList implementation node of devixpress adds a custom right-click menu and implements the node deletion function:

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

In the above, the user-defined right-click menu has been implemented to delete the tree node. Here, the user-defined right-click menu is implemented

Right click function customization, such as opening the file selection box.

Note:

Blog home page:
https://blog.csdn.net/badao_liumang_qizhi
Pay attention to the public address
Domineering procedural ape
Get programming related ebooks, tutorials and free downloads.

Realization

Bind treelist's mouse click event first

treeList.MouseClick -= treeList_MouseClick;
treeList.MouseClick += treeList_MouseClick;

 

Then in the bound click event

Get treelist, then get its data source and convert it to object List, then relevant business judgment.

Then, if you right-click, add a new right-click menu item.

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;                                //Enable full line selection

                            #region Right click to pop up context menu - Delete data file

                            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 = "delete";
                                mnuDelete.Click += delegate(object s, EventArgs ea) {
                                    DialogResult dialogResult = DevExpress.XtraEditors.XtraMessageBox.Show(String.Format("Are you sure you want to delete this experiment data[{0}]?\r\n Delete

//Unable to recover after removal!", nodeData.Id), "Bossy title", System.Windows.Forms.MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                                    if (dialogResult == DialogResult.Yes)
                                    {
                                        try
                                        {
                                            string fileName = String.Empty;
     

                                            #region Delete the corresponding tree node

                                            DevExpress.XtraTreeList.Nodes.TreeListNode selectedNode = treeList.FindNodeByKeyID(nodeData.Id);
                                            if (selectedNode != null)
                                            {
                                                selectedNode.ParentNode.Nodes.Remove(selectedNode);
                                            }

                                            #endregion

                                            treeList.OptionsSelection.EnableAppearanceFocusedRow = false;                                //Disable whole line selection
                                        }
                                        catch(Exception ex)
                                        {
                                          
                                            DevExpress.XtraEditors.XtraMessageBox.Show("Exception in deleting experimental data:" + ex.Message, "Bossy title", MessageBoxButtons.OK,

MessageBoxIcon.Error);
                                        }
                                    }
                                };
                                ctxMenu.MenuItems.Add(mnuDelete);
                                #endregion

                                #region Right click to pop up context menu - Import profile
                                System.Windows.Forms.MenuItem mnuImport = new System.Windows.Forms.MenuItem();
                                mnuImport.Text = "Import profile";
                                mnuImport.Click += delegate(object s, EventArgs ea)
                                {
                                    OpenFileDialog importOpenFileDialog = new OpenFileDialog();
                                    importOpenFileDialog.ShowDialog();
                                   
                                };
                                ctxMenu.MenuItems.Add(mnuImport);

                                #endregion

                                #region Right click to pop up context menu - Export profile
                                System.Windows.Forms.MenuItem mnuExport = new System.Windows.Forms.MenuItem();
                                mnuExport.Text = "Export profile";
                                mnuExport.Click += delegate(object s, EventArgs ea)
                                {
                                    DialogResult dialogResult = DevExpress.XtraEditors.XtraMessageBox.Show(String.Format("export[{0}]Success!", nodeData.Id), "Title

", 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;                                //Disable whole line selection
            }
        }

 

Effect

 

 

After clicking import profile

Tags: C# Windows Programming

Posted on Wed, 06 Nov 2019 12:28:57 -0800 by simmosn