문제

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

도움이 되었습니까?

해결책 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   
    }
}

다른 팁

Maybe ContextMenuStrip.SourceControl is what you want.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top