contextMenuStrip with TreeNodes

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.
 
J

JJ

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
 

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