Solved

right click context menu in treeview

Posted on 2004-08-27
6
606 Views
Last Modified: 2009-12-16
This seems like it should be easy to do but has me stumped.

I have a treeview.  I want to create a right-click context menu whereby the user can add or delete a node.  I am having trouble just identifying the node that the user right clicked.

Here is my code so far.  (Right now, I am just trying to display the node name in a text box.  If I can get that to work, the add/delete logic is easy).

I am using global variables to track mouse position as the contextmenu popup event doesn't appear to provide the location.  But this isn't working correctly.  Basically, the location is wrong.

In fact, I'd prefer to do it without the global variables, but will settle for any way of getting it to work right now.

    Private Sub ContextMenu1_Popup(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup
        MsgBox(TreeView1.GetNodeAt(TreeView1.PointToScreen(New Point(MouseUpX, MouseUpY))).Text)
    End Sub

    Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown
        MouseUpX = e.X
        MouseUpY = e.Y()
    End Sub
0
Comment
Question by:chlade
  • 3
  • 2
6 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11915582
from: http://www.syncfusion.com/FAQ/WinForms/FAQ_c91c.asp#q1097q which has other good info ...

24.12 How can I display a context menu when the user right-clicks on a node in the TreeView control?    

You can display a context menu when a user right-clicks on a node by listening to the TreeView's MouseUp event as shown below:
 
[C#]
 
private void treeView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
 
{
 
     if(e.Button == MouseButtons.Right)
 
     {
 
          Point ClickPoint = new Point(e.X,e.Y);
 
          TreeNode ClickNode = treeView1.GetNodeAt(ClickPoint);
 
          if(ClickNode == null) return;
 
          // Convert from Tree coordinates to Screen coordinates
 
          Point ScreenPoint = treeView1.PointToScreen(ClickPoint);
 
          // Convert from Screen coordinates to Formc coordinates
 
          Point FormPoint = this.PointToClient(ScreenPoint);
 
          // Show context menu
 
          contextmenu.MenuItems.Clear();
 
          contextmenu.MenuItems.Add("Item1");
 
          contextmenu.MenuItems.Add("Item2");
 
          contextmenu.Show(this,FormPoint);
 
     }
 
}
 

 
[VB.NET]
 
Private Sub treeView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
 
     If e.Button = MouseButtons.Right Then
 
          Dim ClickPoint As Point = New Point(e.X,e.Y)
 
          Dim ClickNode As TreeNode = treeView1.GetNodeAt(ClickPoint)
 
          If ClickNode Is Nothing Then
 
                Return
 
          End If
 
          ' Convert from Tree coordinates to Screen coordinates
 
          Dim ScreenPoint As Point = treeView1.PointToScreen(ClickPoint)
 
          ' Convert from Screen coordinates to Formc coordinates
 
          Dim FormPoint As Point = Me.PointToClient(ScreenPoint)
 
          ' Show context menu
 
          contextmenu.MenuItems.Clear()
 
          contextmenu.MenuItems.Add("Item1")
 
          contextmenu.MenuItems.Add("Item2")
 
          contextmenu.Show(this,FormPoint)
 
     End If
 
End Sub
 
0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 500 total points
ID: 11915634
Private Sub TreeView1_MouseDown( _
    ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles TreeView1.MouseDown

    ' This example code shows two important treeview concepts:
    ' 1. How to get a handle to the node that was clicked on.
    ' 2. How to set focus to a node when it is right-clicked.
    '
    Dim NodeClicked As TreeNode

    ' Get the node clicked on
    NodeClicked = Me.TreeView1.GetNodeAt(e.X, e.Y)

    ' Was a node clicked on
    If Not NodeClicked Is Nothing Then
        Console.WriteLine("Node is: " & NodeClicked.Text)
    Else
        Console.WriteLine("No node clicked.")
    End If

    ' By default, right-clicking on a node does not set focus to that node.
    ' This is especially important when you want to use a ContextMenu control
    ' and associate it with a TreeView.  This code shows how to do it.
    If e.Button = MouseButtons.Right Then
        Me.TreeView1.SelectedNode = NodeClicked
    End If

End Sub
0
 

Author Comment

by:chlade
ID: 11915679
Thanks, iboutchkine.  That looks like what I need.  I'll try it and let you know.

Would it be safe to assume then that I can use the treeview1.selected node to identify the node to delete in the click event of my context menu?  I can't think of why this wouldn't work.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11915702
is this really different than what I posted ? except for the fact that the code I posted checks that you right clicked BEFORE doing the work and actually displays a context menu ?
0
 

Author Comment

by:chlade
ID: 11915781
That did it.

gregoryyoung, I looked at yours first and couldn't tell what was what.  It really didn't lead me to figure out how to do it.  All I really saw was a lot of code on how to create a context menu through code.  With the help of the second example, which was much more clear to me, I was able to solve it.  Looking back, I can see how yours did that too, but it wasn't clear to me.  

The documentation and simplicity of the second example was much better help to me.

Thanks very much to both of you though.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11916333
didnt "lead you" to figure out how to do it... what are you taling about ? all you had to do was copy/paste the code and change the name of the treeview as it already DID it including the context menu, it was a complete answer vs a partial answer probably why it was more code ...

please do not expect further assistance from me on any issue.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question