0

我正在寻找一种方法来为程序提供导致 ContextMenuStrip 打开的控件。我正在为多个组合框分配相同的条以重复使用它们,就像在课程开始时一样。

myComboBox.ContextMenuStrip = changevaluestrip;

这里我有“添加值”和“删除值”,当然它们中的每一个都必须知道它必须从哪个组合框中删除值。我试着用

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

但这并没有真正奏效,只是给了我“删除价值”作为发件人......

4

2 回答 2

1

也许ContextMenuStrip.SourceControl是你想要的。

于 2013-09-02T11:00:55.017 回答
0

是的,@KingKing 是对的。您需要ContextMenuStrip.SourceControl用于此目的。这是为您执行此操作的代码段。

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   
    }
}
于 2013-09-02T11:33:21.400 回答