場景 DevExpress的TreeList實現節點上添加自定義右鍵菜單並實現刪除節點功能: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102551175 在上面已經實現自定義右鍵菜單刪除樹節點,這裡要實現自定義右鍵菜單實現 ...
場景
DevExpress的TreeList實現節點上添加自定義右鍵菜單並實現刪除節點功能:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102551175
在上面已經實現自定義右鍵菜單刪除樹節點,這裡要實現自定義右鍵菜單實現
右鍵功能自定義,比如打開文件選擇框。
註:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關註公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載。
實現
首先綁定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; //禁用整行選中 } }
效果
點擊導入配置文件後