简体   繁体   English

如何解决Treeview单击的上下文菜单问题

[英]How can i fix the issue for context menu for Treeview click

My code for opening a context menu on Right-click for a treeview is as follows 我的右键单击以打开树视图的上下文菜单的代码如下

private void contextMenu_Opening(object sender, CancelEventArgs e)
    {
        if (Convert.ToInt16(tvwACH.SelectedNode.Tag) == 1)
        {
            contextMenu.Items.Add(New);
            contextMenu.Items.Remove(Remove);
            contextMenu.Items.Remove(Saveas);
            contextMenu.Items.Remove(Save);
            contextMenu.Items.Remove(addEntry);
        }
        if (Convert.ToInt16(tvwACH.SelectedNode.Tag) == 2)
        {
            contextMenu.Items.Add(New);
            contextMenu.Items.Remove(Remove);
            contextMenu.Items.Remove(Saveas);
            contextMenu.Items.Remove(Save);
            contextMenu.Items.Remove(addEntry);
            New.Text = "Add FileHeader";
        }
        if (Convert.ToInt16(tvwACH.SelectedNode.Tag) == 3)
        {
            contextMenu.Items.Remove(New);
            contextMenu.Items.Add(Save);
            contextMenu.Items.Add(Saveas);
            contextMenu.Items.Remove(Remove); //Added later
            contextMenu.Items.Remove(addEntry);
        }
        if (tvwACH.SelectedNode.Parent != null)
        {
            string str = tvwACH.SelectedNode.Parent.ToString().Substring(10);
            if (str == "BatchHeader")
            {
                contextMenu.Items.Remove(New);
                contextMenu.Items.Remove(Save);
                contextMenu.Items.Remove(Remove);
                contextMenu.Items.Remove(Saveas);
                contextMenu.Items.Add(addEntry);
            }
        }

and also mouse down for treeview as follows 并按下鼠标以查看树视图,如下所示

 private void tvwACH_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            contextMenu.Show(tvwACH, e.Location);
        }
        location = e.Location;
    }

But this getting opened every where on tree-view control but i only need it to be opened when i click on Nodes of my tree-view. 但这会在树视图控件的所有位置打开,但是我只需要在单击树视图的节点时才将其打开。

Any help please 任何帮助请

If you only want the context menu to be displayed when the user right-clicks on a node, you need to include some logic in your MouseDown event hander to verify that the click event occurred over a node. 如果只希望在用户右键单击节点时显示上下文菜单,则需要在MouseDown事件处理程序中包含一些逻辑以验证单击事件是否发生在节点上。

You can determine the node that is located at a particular point using the HitTest method provided by the TreeView . 您可以使用TreeView提供的HitTest方法确定位于特定点的节点。 For example, you could modify your current MouseDown event handler to include the following code: 例如,您可以修改当前的MouseDown事件处理程序以包括以下代码:

private void tvwACH_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        if (tvwACH.HitTest(e.Location).Node != null)
        {
            contextMenu.Show(tvwACH, e.Location);
        }
    }
}

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

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