0

I am trying to fix a problem where if a user right clicks on a selection, it will not select/highlight it, and if, for example. "delete", is selected, it deletes the previous selection that was clicked on. I have read many posts about using the mouse_down event, but nothing I have tried has seemed to work for me. Here is the current code:

private void treelocations_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        Point pos = new Point();
        pos.X = e.X;
        pos.Y = e.Y;
        mnulocation.Show(this.treelocations, pos);
    }
}

I simply would like to be able to right click on any selection in the list and have it highlight/select that record.

Damith
  • 62,401
  • 13
  • 102
  • 153
C-Sharp-Noob
  • 25
  • 1
  • 1
  • 7
  • Have you tried to put the breakpoint inside this EventHadler? Does it works? If no, maybe some errors? Is your EventHadler connected in form constructor? Also, maybe this thread is related to your problem - http://stackoverflow.com/questions/4784258/right-click-select-on-net-treenode?rq=1? – Mr Zak May 15 '13 at 19:39
  • I've often found the easiest way to interact with `Nodes` is to handle all the mouse events in the `TreeView` as you are doing and use [`TreeView.GetNodeAt(Point)`](http://msdn.microsoft.com/en-us/library/1yxbz43s.aspx) to get the node the user is pointing at. (You can pass it the coords in the `MouseEventArgs` as they are relative to the `TreeView` allready) – VisualMelon May 15 '13 at 19:46
  • I will post something that works and you can use a switch statement to control the different things you want to do you basically have the code you are using in the wrong event it should be on the `TreeNodes MouseClick Event` – MethodMan May 15 '13 at 19:53
  • also is this your own method `mnulocation.Show(this.treelocations, pos);` if not `.Show` does not take any parameters can you clarify what this is and or show how you have implemented / defined it..? – MethodMan May 15 '13 at 20:01

2 Answers2

0

You will need to change the selected node upon firing the mouse down event.

Take a look at: Selected Node Property

Or your could take a look at: NodeMouseClick event.

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            treeView1.Nodes.Remove(e.Node);
        }
    }
JohnC1
  • 841
  • 1
  • 12
  • 27
0

You can add in the other case statement as needed but this should help you get what you need

 private void treelocations_MouseClick(object sender, MouseEventArgs e)
 {
      switch(e.Button)
      {
          case MouseButtons.Right:
          {
              Point pos = new Point();
              pos.X = e.X;
              pos.Y = e.Y;
              treeView1.Focus();
              MessageBox.Show(treelocations.SelectedNode.Text);
              break;
           }
      }
 }
MethodMan
  • 18,625
  • 6
  • 34
  • 52