在treenode鼠标右键单击时没有显示上下文 [英] contex menue not showing on treenode mouse right click

查看:83
本文介绍了在treenode鼠标右键单击时没有显示上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi


i am unabel显示我在下面的代码使用的contexmenue条带



ContextMenuStrip abc = new ContextMenuStrip( );

if(e.Button == MouseButtons.Right)

{

treeView1.SelectedNode = e.Node;

abc.Show(treeView1,e.Location);

}

请帮帮我。

hi
i am unabel to show the contexmenue strip i am using below code

ContextMenuStrip abc = new ContextMenuStrip();
if (e.Button == MouseButtons.Right)
{
treeView1.SelectedNode = e.Node;
abc.Show(treeView1, e.Location);
}
please help me out.

推荐答案

Win Form ContextMenuStrip是一个组件,意味着与整个Control或多个控件相关联。



如果设置了ContextMenuStrip属性TreeView使用您在设计时添加到Form的ContextMenuStrip,然后只要您在TreeView的DisplayRectangle边界中单击任何地方,它就会出现:那是独立的选择任何节点或节点。



如果您希望限制ContextMenu的显示,请在此处一个只在上下文单击的TreeNode碰巧是TreeView的SelectedNode时才会在上下文单击时显示它的示例:
A Win Form ContextMenuStrip is a "Component" meant to be associated with an entire Control, or multiple Controls.

If you set the ContextMenuStrip Property of the TreeView to use the ContextMenuStrip you've added to the Form at design-time, then it's going to appear whenever you context-click anywhere in the TreeView's DisplayRectangle bounds: that's independent of any Node, or Nodes, being Selected.

If you wish to limit the display of the ContextMenu, here's an example that will only show it on context-click when the TreeNode you context-click on happens to be the SelectedNode of the TreeView:
// define at Form scope
private TreeNode currentRightClickedTreeNode;
private bool IsContextMenuHidden = false;

// handle the TreeView MouseDown
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
    IsContextMenuHidden = false;

    if (e.Button == MouseButtons.Right)
    {
        currentRightClickedTreeNode = treeView1.GetNodeAt(e.Location);
        IsContextMenuHidden = currentRightClickedTreeNode == null || currentRightClickedTreeNode != treeView1.SelectedNode;
    }
}

// handle the ContextMenu Opening Event
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
    e.Cancel = IsContextMenuHidden;
}

这可以通过测试确定是否显示ContextMenu的两个条件来实现:



1.上下文-click发生在所有TreeNodes外部某处



2.上下文单击是否发生在未选中的TreeNode上

This works by testing for two conditions that determine whether or not the ContextMenu is shown:

1. does the context-click occur somewhere "outside" all TreeNodes

2. does the context-click occur on a TreeNode that is not selected


这篇关于在treenode鼠标右键单击时没有显示上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆