DevExpress的TreeList实现自定义右键菜单打开文件选择对话框
生活随笔
收集整理的這篇文章主要介紹了
DevExpress的TreeList实现自定义右键菜单打开文件选择对话框
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
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);}#endregiontreeList.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);#endregionctxMenu.Show(treeList, new System.Drawing.Point(e.X, e.Y));}return;}}}treeList.OptionsSelection.EnableAppearanceFocusedRow = false;??????????????????????????????? //禁用整行選中}}效果
?
點擊導入配置文件后
?
總結
以上是生活随笔為你收集整理的DevExpress的TreeList实现自定义右键菜单打开文件选择对话框的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DevExpress的下拉框控件Comb
- 下一篇: Winform中在使用Dock属性设计页