首页 > 代码库 > [Dev]DevExpress之treelist右键菜单实现
[Dev]DevExpress之treelist右键菜单实现
加入BarManager和popupMenu组件:
设置popupMenu组件的Manager属性:
右键点击popupMenu主键,点击Customize选项加入菜单
代码:
using System; using System.Collections.Generic; using System.Linq; using System.Data; using DevExpress.XtraTreeList; using DevExpress.XtraTreeList.Nodes; using DZAMS.DBUtility; using System.Windows.Forms; using DevExpress.XtraEditors; using System.Drawing; namespace DZAMS.Admin { public partial class MenuInfo_Frm : DevExpress.XtraEditors.XtraForm { public MenuInfo_Frm() { InitializeComponent(); } private void MenuInfo_Frm_Load(object sender, EventArgs e) { string strSql = "select * from dbo.DZ_MenuInfo order by PARENTID,ID,SORTINDEX ASC"; DataTable tblDatas = SqlHelper.ExecuteDataset(SqlHelper.conn, CommandType.Text, strSql).Tables[0]; //设置字段 treeList1.KeyFieldName = "ID"; treeList1.ParentFieldName = "PARENTID"; treeList1.DataSource = tblDatas; //递归设置图标 //DevTreeListView.SetImageIndex(tlCategory, null, 1, 0); treeList1.ExpandAll(); treeList1.LookAndFeel.UseDefaultLookAndFeel = false; treeList1.LookAndFeel.UseWindowsXPTheme = true; } private void treeList1_DoubleClick(object sender, EventArgs e) { TreeListNode clickedNode = this.treeList1.FocusedNode; string name = clickedNode.GetDisplayText("NAME"); string formname = clickedNode.GetDisplayText("FORMNAME"); } private void treeList1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Right) { treeList1.ContextMenuStrip = null; TreeListHitInfo hInfo = treeList1.CalcHitInfo(new Point(e.X, e.Y)); TreeListNode node = hInfo.Node; treeList1.FocusedNode = node; if (node != null) { treeList1.ContextMenuStrip = contextMenuStrip1; } } } private void treeList1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { TreeList tree = sender as TreeList; if (e.Button == MouseButtons.Right && ModifierKeys == Keys.None && treeList1.State == TreeListState.Regular) { Point p = new Point(Cursor.Position.X, Cursor.Position.Y); TreeListHitInfo hitInfo = tree.CalcHitInfo(e.Location); if (hitInfo.HitInfoType == HitInfoType.Cell) { tree.SetFocusedNode(hitInfo.Node); } if (tree.FocusedNode != null) { popupMenu1.ShowPopup(p); } } } private void barbtnTop_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { } private void barbtnSub_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { } private void barbtnModify_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { } private void barbtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { TreeListNode clickedNode = this.treeList1.FocusedNode; string name = clickedNode.GetDisplayText("NAME"); string formname = clickedNode.GetDisplayText("FORMNAME"); XtraMessageBox.Show("您确定要删除" + formname+"吗?", "警告"); } } }
效果:
[Dev]DevExpress之treelist右键菜单实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。