ContextMenuStrip owner

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In my program I dynamically create treeViews and dynamically add nodes to them.
To each node I attach ContextMenuStrip which suppose allow delete selected
node
e.t.c.

How from inside click event handling function to find which node was selected?
sender is set to ToolStripItem clicked.
 
Hi lenusia,

You can retrieve the source by retrieving the ContextMenuStrip from the
ToolStripItem. You can retrieve the source control (the treeview you are
looking for) from the ContextMenuStrip.SourceControl. Like:
ToolStripMenuItem tsItem = (ToolStripMenuItem)sender;
ContextMenuStrip cMenuStrip = (ContextMenuStrip)tsItem.Owner;
Control theSourceControl = cMenuStrip.SourceControl;
TreeView myTreeView = (TreeView)theSourceControl;
MessageBox.Show(myTreeView.Name);

Note: you realy should check your objects before making explicit casts,
unless you are 100% shure nothing else could have raised the event.

Regards,

Nico
 

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

Back
Top