chlade
asked on
right click context menu in treeview
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 .PointToSc reen(New Point(MouseUpX, MouseUpY))).Text)
End Sub
Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.Mouse EventArgs) Handles TreeView1.MouseDown
MouseUpX = e.X
MouseUpY = e.Y()
End Sub
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
End Sub
Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.Mouse
MouseUpX = e.X
MouseUpY = e.Y()
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
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 ?
ASKER
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.
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.
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.
please do not expect further assistance from me on any issue.
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.Mouse
{
if(e.Button == MouseButtons.Right)
{
Point ClickPoint = new Point(e.X,e.Y);
TreeNode ClickNode = treeView1.GetNodeAt(ClickP
if(ClickNode == null) return;
// Convert from Tree coordinates to Screen coordinates
Point ScreenPoint = treeView1.PointToScreen(Cl
// Convert from Screen coordinates to Formc coordinates
Point FormPoint = this.PointToClient(ScreenP
// Show context menu
contextmenu.MenuItems.Clea
contextmenu.MenuItems.Add(
contextmenu.MenuItems.Add(
contextmenu.Show(this,Form
}
}
[VB.NET]
Private Sub treeView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.Mouse
If e.Button = MouseButtons.Right Then
Dim ClickPoint As Point = New Point(e.X,e.Y)
Dim ClickNode As TreeNode = treeView1.GetNodeAt(ClickP
If ClickNode Is Nothing Then
Return
End If
' Convert from Tree coordinates to Screen coordinates
Dim ScreenPoint As Point = treeView1.PointToScreen(Cl
' Convert from Screen coordinates to Formc coordinates
Dim FormPoint As Point = Me.PointToClient(ScreenPoi
' Show context menu
contextmenu.MenuItems.Clea
contextmenu.MenuItems.Add(
contextmenu.MenuItems.Add(
contextmenu.Show(this,Form
End If
End Sub