Solved

right click context menu in treeview

Posted on 2004-08-27
6
587 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now