場景
Winform控制元件-DevExpress18下載安裝注冊以及在VS中使用:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100061243
DevExpress的TreeList怎樣設定資料源,從實體入手:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102548490
實作了樹形結構后,我們要實作在樹形節點上點擊右鍵時彈窗,彈窗確認之后實作將當前節點洗掉,
注:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載,
實作
這里在表單加載完的事件里系結Treelist的滑鼠點擊事件,
this.treeList2.MouseClick += treeList2_MouseClick;在事件方法中
private void treeList2_MouseClick(object sender, MouseEventArgs e) { //獲取當前TreeList物件 DevExpress.XtraTreeList.TreeList treeList = sender as DevExpress.XtraTreeList.TreeList; if (treeList != null && treeList.Selection.Count == 1) { object idValue = https://www.cnblogs.com/badaoliumangqizhi/p/null; string strIdValue =https://www.cnblogs.com/badaoliumangqizhi/p/ String.Empty; DataTreeNode nodeData = null; //通過TreeList的DataSource屬性獲取所有節點的List List<DataTreeNode> datasource = treeList.DataSource as List<DataTreeNode>; if (datasource != null) { //獲取選中節點的集合中第一個的Id屬性 idValue = https://www.cnblogs.com/badaoliumangqizhi/p/treeList.Selection[0].GetValue("Id"); strIdValue = idValue.ToString(); //獲取當前選中節點DataTreeNode物件 nodeData = https://www.cnblogs.com/badaoliumangqizhi/p/datasource.Where(p => p.Id == strIdValue).FirstOrDefault (); if (nodeData != null) { //獲取或設定是否啟用聚焦節點的外觀設定 -啟用整行選中 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) { //DevExpress的對話框 DialogResult dialogResult = DevExpress.XtraEditors.XtraMessageBox.Show(String.Format("確定要洗掉此實驗資料嗎[{0}]?\r\n洗掉后無法恢復!", nodeData.Id), "標題", System.Windows.Forms.MessageBoxButtons.YesNo, MessageBoxIcon.Question); //如果點擊了Yes if (dialogResult == DialogResult.Yes) { try { string fileName = String.Empty; #region 洗掉對應的樹節點 //通過設定主鍵ID找到節點node DevExpress.XtraTreeList.Nodes.TreeListNode selectedNode = treeList.FindNodeByKeyID(nodeData.Id); if (selectedNode != null) { //洗掉節點 this.treeList2.Nodes.Remove(selectedNode); } #endregion //禁用整行選中 treeList.OptionsSelection.EnableAppearanceFocusedRow = false; } catch (Exception ex) { ICSharpCode.Core.LoggingService<DataTreeListHelper>.Error("洗掉實驗資料例外:" + ex.Message, ex); DevExpress.XtraEditors.XtraMessageBox.Show("洗掉實驗資料例外:" + ex.Message, "標題",MessageBoxButtons.OK, MessageBoxIcon.Error); } } }; ctxMenu.MenuItems.Add(mnuDelete); ctxMenu.Show(treeList, new System.Drawing.Point(e.X, e.Y)); } #endregion return; } } treeList.OptionsSelection.EnableAppearanceFocusedRow = false; //禁用整行選中 } }
具體注釋見代碼,
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/3542.html
標籤:WinForm
