-1

I have a TreeView with a node.

Selection is disabled for it with:

Private Sub TreeList_BeforeSelect(sender As Object, e As TreeViewCancelEventArgs) Handles TreeList.BeforeSelect
        e.Cancel = True
    End Sub

I was not able to find a way, to find out which node (as there will be multiple) opens the ContextMenuStrip (so I could delete it with it).

As per comments, now it works:

Dim WhichItemIsIt As TreeNode
Private Sub TreeList_NodeMouseClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeList.NodeMouseClick
    WhichItemIsIt = e.Node
End Sub
Private Sub RemoveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RemoveToolStripMenuItem.Click
    TreeList.Nodes.Remove(WhichItemIsIt)
End Sub
CruleD
  • 1,153
  • 2
  • 7
  • 15

1 Answers1

1

I would think that this would do the trick:

Private lastClickedNode As TreeNode

Private Sub TreeView1_NodeMouseClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
    lastClickedNode = e.Node
End Sub

Right-clicking a node will assign it to that field before the menu is displayed and you can then access that node from the Click event handler of a menu item or whatever.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Hmm, I already tried that before I made this question, but it doesn't work. Dim WhichItemIsIt As TreeNode, Private Sub List_NodeMouseClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles List.NodeMouseClick, Dim WhichItemIsIt As TreeNode = e.Node, End Sub – CruleD Jun 16 '18 at 10:50
  • The issue is when code enters (or even starts entering) Private Sub RemoveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RemoveToolStripMenuItem.Click, List.Nodes.Remove(WhichItemIsIt), End Sub. WhichItemIsIt is already Nothing. – CruleD Jun 16 '18 at 10:57
  • Take note that NodeMouseClick event does set WhichItemIsIt to node, at least temporary. – CruleD Jun 16 '18 at 11:00
  • Extended code is basically unreadable in comments. Please update your question with the relevant code and information. That said, How can the field be `Nothing` unless you set it to `Nothing`? If you are setting it to `Nothing`, where and why? – jmcilhinney Jun 16 '18 at 11:46
  • Done, "WhichItemIsIt" is something at End Sub of NodeMouseClick event. At the very next line (Private Sub RemoveToolStripMenuItem_Click...) it is Nothing. – CruleD Jun 16 '18 at 13:23
  • 1
    The issue is that you are declaring a local variable and assigning to that instead of assigning to the member variable. You've got two different variables with the same name and you're assigning to one and expecting to get something back from the other one. Rather than `Dim WhichItemIsIt As TreeNode = e.Node` it should just be `WhichItemIsIt = e.Node`. You should have been able to see that difference between your code and mine without my having to point it out. – jmcilhinney Jun 16 '18 at 14:53
  • Hmm indeed, that is a leftover from something else I was trying out. Now that I removed it, it works, so thanks for pointing it out. I guess I was just too tired to notice something so obvious. – CruleD Jun 16 '18 at 14:59