简体   繁体   English

C#,treeView,如何更改标准的右键单击行为

[英]C#, treeView, How to change standard right-click behavior

I have a question. 我有个问题。

I put a treeview control on my form and added some nodes. 我在我的表单上放了一个treeview控件并添加了一些节点。

    public Form1()
    {
        InitializeComponent();

        treeView1.Nodes.Add("root node #1");
        treeView1.Nodes.Add("root node #2");
        treeView1.Nodes.Add("root node #3");
        treeView1.Nodes.Add("root node #4");
        treeView1.Nodes.Add("root node #5");
    }

I want to change standard right-click behavior. 我想更改标准的右键单击行为。 When I right-clicked on a tree Node then Treeview changed for a while selectedIndex. 当我右键单击树节点时,Treeview更改了一段时间selectedIndex。 I don't want it. 我不想要它。 How can I fix standard behavior ? 我该如何修复标准行为?

In ideal, it would be: right click on a treenode text --> context menu appears, right click anywhere outside treenode text --> (absolutely) nothing happens . 在理想情况下,它将是:右键单击treenode文本 - >上下文菜单出现,右键单击treenode文本外的任何位置 - >(绝对)没有任何反应

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
 if (e.Button == MouseButtons.Right)
 {
    // put your logic here like
    // ContextMenu1.Show();
 }
}

Alex, Try this. 亚历克斯,试试吧。 The BeforeSelect handlers event args Cancel is tied to the fact that the right mouse is down. BeforeSelect处理程序事件args取消与鼠标右键关闭的事实有关。 This suppresses the firing of the SelectedIndex changed. 这会抑制SelectedIndex的触发发生变化。 The MouseDown tracks if the right mouse is depressed and displays the context menu. MouseDown跟踪是否按下右键并显示上下文菜单。 The display is safe to move to MouseUp instead of MouseDown. 显示器可以安全地移动到MouseUp而不是MouseDown。 The MouseUp clears the flag indicating that the RightMouse button is depressed. MouseUp清除表示按下RightMouse按钮的标志。

All of this information on how I did this is available on MSDN. 有关如何执行此操作的所有这些信息都可以在MSDN上找到。 The trick is actually reading the names of all of the events -- Yes I know there are a lot -- then making a list of the "interesting ones" in your case you named SelectedIndex changing, and Mouse clicks. 诀窍实际上是读取所有事件的名称 - 是的我知道有很多 - 然后在你的案例中列出“有趣的”列表,你命名为SelectedIndex更改和鼠标点击。 That immediately limits the event names you should read in detail on... If you want the text to not highlight while you're right clicking... well that's a different matter entirely and I caution you against it as it's valuable user feedback. 这会立即限制您应该详细阅读的事件名称...如果您希望文本在您点击右键时不突出显示......那么这完全是另一回事,我提醒您反对它,因为它是有价值的用户反馈。

    bool isRBut = false;
    private void treeView1_MouseDown(object sender, MouseEventArgs e)
    {
        isRBut = e.Button == MouseButtons.Right;
        if (isRBut)
        {
            TreeViewHitTestInfo hti =treeView1.HitTest(e.Location);
            if (hti.Location == TreeViewHitTestLocations.Label)                
                contextMenuStrip1.Show(treeView1, new Point(hti.Node.Bounds.Left, hti.Node.Bounds.Bottom));                
        }
    }

    private void treeView1_MouseUp(object sender, MouseEventArgs e)
    {
        isRBut = e.Button == MouseButtons.Right;
    }

    private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
    {
        e.Cancel = isRBut;
    }

Additionally, here's a bit of human language trivia for you. 另外,这里有一些人类语言琐事。 Hopefully this will help you in the future. 希望这将在未来帮助您。 Phrases such as "No, no, no" are interpreted by native English speakers as very rude. 诸如“不,不,不”之类的短语被英语母语人士解释为非常粗鲁。 Just do your best to list the behavior you see, and the behavior you want. 只要尽力列出您看到的行为以及您想要的行为。 Even if people misunderstand stick to the facts only and leave out the obvious signs of frustration. 即使人们误解只是坚持事实而忽略了明显的挫折迹象。 This will help you get what you're after. 这将帮助您获得所需的信息。 As well on SO if someone has a habit of not accepting answers many members here will have a habit of not providing future answers to such members. 如果某人有不接受答案的习惯,那么很多成员都会习惯不向这些成员提供未来的答案。

Override the MouseClick event, and in the event check if the click was right click 覆盖MouseClick事件,并在事件中检查单击是否是右键单击

    private void treeView1_MouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            //Do something
        }
    }

You need to handle the NodeMouseClick event and check the right mouse button was clicked: 您需要处理NodeMouseClick事件并检查单击鼠标右键:

treeView1.NodeMouseClick += (o, e) => {
    if(e.Button == MouseButtons.Right)
    {
        //show menu...
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM