intellectual argument about context menus

E

Eric Newton

Ok, given the following VB code:
Private Sub TreeView1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseUp
If e.Button And MouseButtons.Right = MouseButtons.Right Then
Dim pt As Point = TreeView1.PointToClient(Form.MousePosition)
Dim node As TreeNode = TreeView1.GetNodeAt(pt)
Dim ctx As New ContextMenu
ctx.MenuItems.Add("Test 1")
ctx.MenuItems.Add(node.Text, AddressOf ContextMenuItem_Click)
ctx.Show(TreeView1, pt)
End If
End Sub
Private Sub ContextMenuItem_Click(ByVal sender As Object, ByVal e As
EventArgs)
'what do you do here? reget the Form.MousePosition node?
'not guaranteed to get the node that was clicked... actually since the
mouse pointer has
'moved now, you are almost gauranteed to NOT get the right node...
'so how do you pass the actual node that was right clicked?
End Sub

just as the comments say, how does one properly hand off which node was
clicked?
in the ContextMenuItem_Click, sender will be the menuitem... so I ask why
the MenuItem type doesnt have a tag... or even the ContextMenu, since the
contextmenu was built (in this case) specific to the node clicked... (in
concept)

so why doesnt COntextMenu or MenuItem or SOMETHING have anything to relate
the click?

The only answer is to subclass the MenuItem or the ContextMenu types and add
this simply trivial but neccesary piece of information... just wondering if
I'm missing something obvious?
 
D

Doug Forster

Hi Eric,

You can just save the coords from the MouseEventArgs (e) from the MouseUp
handler into a member variable. This is not just an intellectual issue but
can actually be quite important (certainly is in my mapping app). Remember
that ctx.Show is modal so the correct value is guaranteed to be there during
execution of the menu handler. The same issue arises in unmanaged programs.

Cheers

Doug Forster
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top