原创

[Dev]DevExpress之treelist右键菜单实现

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://david.blog.csdn.net/article/details/50675231

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

效果:



文章最后发布于: 2016-02-16 15:56:33
展开阅读全文
0 个人打赏
私信求帮助

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 鲸 设计师: meimeiellie

分享到微信朋友圈

×

扫一扫,手机浏览