梦向心海 发表于 2023-12-22 11:06:26

【c# winform】devexpress treeList右键菜单添加按钮

本文提供俩种不需要手动添加编辑控件方法。
方法一:创建新的右键菜单添加“执行选择”按钮,且抑制TreeList自带菜单
结果展示:

 代码: 
private void Form1_Load(object sender, EventArgs e)
{
CreateBarButtonItem();
}

private void CreateBarButtonItem() {

// 创建右键菜单
ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
// 添加 "执行选择" 菜单项
ToolStripMenuItem execSelectedItem = new ToolStripMenuItem("执行选择");
execSelectedItem.Click += ExecSelectedItem_Click; ;
contextMenuStrip.Items.Add(execSelectedItem);
// 关联右键菜单与 TreeList 控件
treeList1.PopupMenuShowing += TreeList1_PopupMenuShowing;
}

private void TreeList1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
{
   // 禁用自带的右键菜单
   e.Allow = false;
}

private void ExecSelectedItem_Click(object sender, EventArgs e)
{
//处理“执行选择”事件逻辑
} 方法二:在TreeList自带菜单基础上添加“执行选择”按钮
结果展示: 

 右键空节点 

 右键节点,保留自带菜单功能  

 右键列标题,保留自带菜单功能‍  
代码: 
private void Form1_Load(object sender, EventArgs e)
{
treeList1.PopupMenuShowing += TreeList1_PopupMenuShowing;//自定义右键展示菜单
}

private void TreeList1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
{
// 获取右键菜单
if (e.Menu is TreeListMenu menu)
{
    // 添加自定义按钮
    DXMenuItem execSelectedItem = new DXMenuItem("执行选择");
    execSelectedItem.Click += ExecSelectedItem_Click;
    // 将自定义按钮插入到菜单的末尾
    menu.Items.Add(execSelectedItem);
}
}

private void ExecSelectedItem_Click(object sender, EventArgs e)
{
//处理“执行选择”事件逻辑


来源:https://www.cnblogs.com/RCJL/archive/2023/12/22/17920545.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 【c# winform】devexpress treeList右键菜单添加按钮