contextMenuStrip with TreeNodes

  • Thread starter Thread starter Paitum
  • Start date Start date
P

Paitum

Dear WindowsForms Community,

I have a question about using ContextMenuStrips with TreeNodes.

Is there a clean OOP way of determining the TreeNode which was right-clicked
on within the Click event-handlers of my ContextMenuStrip?

private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
// sender is a ToolStripMenuItem object
// e contains no information
// contextMenuStrip.SourceControl is a TreeView
}

One would expect the contextMenuStrip.SourceControl to provide the TreeNode
registered to the menu, but instead it provides the TreeView.
 
I don't fully understand your question. I'm assuming that at the time the
right-click occurs the current TreeView.SelectedNode may/may not be the node
you want.
With this assumption, you can use the mouse location from your event to
locate the treenode under it. Of course the mouse location will be inside of
your ContextMenuStrip so you'll have add couple lines to map the mouse
location into the treeview and find the node.

Good luck
Jeff www.ruamkwamkid.com
 
Back
Top