Question

I'm looking for a way to give the program the control which caused the ContextMenuStrip to open. I'm assigning the same Strip for multiple comboboxes to reuse them, like this at the start of the class.

myComboBox.ContextMenuStrip = changevaluestrip;

Here I have "Add Value" and "Delete Value", and of course each of these has to know which from which combobox it has to delete the value. I tried doing it with

private void removeValueToolStrip_Click(object sender, EventArgs e)
{
    ToolStripMenuItem usedbox = sender as ToolStripMenuItem;
    var parent = usedbox.GetCurrentParent();
    DialogResult res = MessageBox.Show("Do you really want to delete this value?", "Delete Value", MessageBoxButtons.YesNo);
    if (res == DialogResult.Yes)
    {
        //Delete it from the combobox it was sent from
    }
}

But that didn't really work out and just gave me "Remove Value" as sender...

Était-ce utile?

La solution 2

Yes, @KingKing is right. You need to use ContextMenuStrip.SourceControl for this purpose. Here is a snippet doing that for you.

ToolStripMenuItem usedbox = sender as ToolStripMenuItem;
ContextMenuStrip parent = usedbox.GetCurrentParent() as ContextMenuStrip;
if (parent != null)
{
    ComboBox combo = parent.SourceControl as ComboBox;
    if (combo != null)
    { 
         //use combobox here   
    }
}

Autres conseils

Maybe ContextMenuStrip.SourceControl is what you want.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top