8

I have a TreeView with the parent node : Node0. I add 3 subnodes:

Node01
Node02
Node03

I have a popup menu that is associate to each of the subnodes.

My problem: If I right-click directly to one of the subnodes, my popup does not display. So I have to Select the Subnode first and Right-click to have the popup displayed.

  1. How can I change the code so that the Direct Right-Click on a specific SubNode open the PopupMenu?
  2. The popupMenu have only OpenMe menu in the list. When clicking on this menu, a windows is supposed to open and this windows should be associated to the submenu I have clicked. How to get the Event of the right-click submenu and display Form with it?

EDIT:

Look at this

private void modifySettingsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            String s = treeView1.SelectedNode.Text;
            new chartModify(s).ShowDialog();
        }
        catch (Exception er)
        {
            System.Console.WriteLine(">>>" + er.Message);
        }
    }

The line String s = treeView1.SelectedNode.Text; gets the name of the selected node and not the node that have been right-clicked.

So here I have to modify this piece of code with the

private void treeview1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                MessageBox.Show(e.Node.Name);
        }

I modify it like this:

try
        {
            TreeNodeMouseClickEventArgs ee;
            new chartModify(ee.Node.Name).ShowDialog();
        }

but it does not work : Error:Use of unassigned local variable 'ee'

EDIT #2: Finaly got the solution

public string s;
private void modifySettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                new chartModify(s).ShowDialog();
            }
            catch (Exception er)
            {
                System.Console.WriteLine(">>>" + er.Message);
            }
        }

and then

private void treeview1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                s = e.Node.Name;
                menuStrip1.Show();
            }
        }

it works,
Thanks

2
  • 3
    Where is your code for the right click ? Commented Jan 8, 2013 at 5:50
  • 2
    Which technology? WinForms? WebForms? ASP.NET MVC? WPF? Commented Jan 8, 2013 at 6:00

2 Answers 2

30

You can try using the NodeMouseClick Event it uses the TreeNodeClickEventArgs to get the Button and the Node that was clicked.

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    if(e.Button == MouseButtons.Right)
        MessageBox.Show(e.Node.Name);
}

Modified Code to show Popup and created Form

public partial class Form1 : Form
{
    string clickedNode;
    MenuItem myMenuItem = new MenuItem("Show Me");
    ContextMenu mnu = new ContextMenu();
    public Form1()
    {
        InitializeComponent();
        mnu.MenuItems.Add(myMenuItem);
        myMenuItem.Click += new EventHandler(myMenuItem_Click);
    }

    void myMenuItem_Click(object sender, EventArgs e)
    {
        Form frm = new Form();
        frm.Text = clickedNode;
        frm.ShowDialog(this);
        clickedNode = "";
    }

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            clickedNode = e.Node.Name;
            mnu.Show(treeView1,e.Location);
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

No what I want is TO Display The PopUpMenu Box related to the Node Selected. First Right-Click on a Node and select ShowMe to display a Form with the Title set to the name of the Node right-clicked.
@DeathCoder is this Winforms or Wpf?
Thank you Mark Hall, your code gives me inpiration. I edit this topic with solution
you can avoid the field altogether if you set treeView1.SelectedNode to e.Node in the NodeMouseClick event. This also has the benefit of selecting the item visually which would not happen for a right click.
3

This will give you the treenode at a particular mouse point when your right click.

if(e.Button == MouseButtons.Right)
        {
            TreeNode destinationNode = ((TreeView)sender).GetNodeAt(new Point(e.X, e.Y));
            //Do stuff
        }

From here you should be able to open a specific popup menu.

1 Comment

No what I want is TO Display The PopUpMenu Box related to the Node Selected. First Right-Click on a Node and select ShowMe to display a Form with the Title set to the name of the Node right-clicked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.