[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+"吗?", "警告");
        }
    }
}

效果:



posted on 2017-06-21 17:23  ljbguanli  阅读(536)  评论(0编辑  收藏  举报