Solved

right click context menu in treeview

Posted on 2004-08-27
6
597 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Receiving a string from a WebService Push 21 47
Advice in Xamarin 21 79
Help with error when uploading excel file 3 28
VB.NET 2008 Publish Error 2 27
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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