如何获取打开ContextMenuStrip的控件
本文关键字:ContextMenuStrip 控件 何获取 获取 | 更新日期: 2023-09-27 18:29:49
我正在寻找一种方法,为程序提供导致ContextMenuStrip打开的控件。我为多个组合框分配相同的Strip以重用它们,就像在类的开头这样。
myComboBox.ContextMenuStrip = changevaluestrip;
这里我有"添加值"answers"删除值",当然每一个都必须知道它必须从哪个组合框中删除值。我试着用
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
}
}
但这并没有真正奏效,只是让我作为发件人"删除值"。。。
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
}
}
也许ContextMenuStrip.SourceControl就是您想要的。