Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ContextMenuStrip

Status
Not open for further replies.

aTekTipsUser

Programmer
Nov 8, 2004
68
US
Is there a way to determine which control opened the ContextMenuStrip? I have 4 labels that share a ContextMenuStrip and I was determining by the ActiveControl. This doesn't work all the time because if I right click one of the labels, then right click a different label while the ContextMenuStrip is open, the ActiveControl is not changed.

I'm using .Net 2.0.

Thanks in advance.
 
Perhaps you would consider posting the answer in case anybody is searching for the samething?

Age is a consequence of experience
 
I suppose it might be beneficial for someone else.

You need to get the contextmenu strip from the toolstrip and from the contextmenu strip use the sourcecontrol as noted in the following code.


private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
//Make sure the sender is a ToolStripMenuItem
ToolStripMenuItem myItem = sender as ToolStripMenuItem;
if (myItem != null)
{
//Get the ContextMenuString (owner of the ToolsStripMenuItem)
ContextMenuStrip theStrip = myItem.Owner as ContextMenuStrip;
if (theStrip != null)
{
//The SourceControl is the control that opened the contextmenustrip.
//In my case it could be a linkLabel
LinkLabel linkLabel = theStrip.SourceControl as LinkLabel;
if (linkLabel == null)
MessageBox.Show("Invalid item selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
if (MessageBox.Show(string.Format("Are you sure you want to remove BOL {0} from this Job?", linkLabel.Text), "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
linkLabel.Text = Program.NullValue(linkLabel);
}
}
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top