Accessing The Control Responsible For A ContextMenuStrip

Posted: 12/27/2007 11:06:29 PM

It certainly isn't easy to access what winform control was ultimately responsible for bringing up a ContextMenuStrip, but it is indeed possible, there is just some casting involved :)


private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (((ContextMenuStrip)((ToolStripMenuItem)sender).Owner).SourceControl ==
        this.PrimeNumbersCheckBoxList)
    {
        this.SelectAllThePrimeNumbers();
    }
   
    if (((ContextMenuStrip)((ToolStripMenuItem)sender).Owner).SourceControl ==
        this.CompositeNumbersCheckBoxList)
    {
        this.SelectAllTheCompositeNumbers();
    }
}




Tags: Tips and Tricks