我有一个ContextMenuStrip,其中一个项目有一个DropDownItems属性,该属性是动态添加的ToolStripMenuItem对象的集合。当我处理子项Click事件时,发送者的类型是ToolStripMenuItem,但它的Owner是ToolStripDropDownMenu。我找不到如何从这里确定“主机”ContextMenuStrip。它没有自己的Owner属性,并且Parent返回null。
当我使用下面@Steve发布的代码的改编版本时:
Dim dropDownItem = DirectCast(sender, ToolStripDropDownItem)
Dim menu As ContextMenuStrip = DirectCast((((dropDownItem.DropDown).OwnerItem).OwnerItem).Owner, ContextMenuStrip)
Dim grid = menu.SourceControl然后menu.SourceControl是Nothing,然而当我处理顶层,即非下拉菜单项的单击时,如下所示
Dim item As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
Dim strip As ContextMenuStrip = DirectCast(item.Owner, ContextMenuStrip)
Dim grid As DataGridView = DirectCast(strip.SourceControl, DataGridView)然后我得到了我想要的网格。
发布于 2012-10-01 18:13:31
如果我理解正确的话,您希望从属于ToolStripDropDownMenu的ToolStripMenuItem的Click事件内部访问ContextMenuStrip对象。
如果是这样的话
private void TestToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripDropDownItem x = sender as ToolStripDropDownItem;
if (x != null)
{
ContextMenuStrip k = (((x.DropDown).OwnerItem).OwnerItem).Owner as ContextMenuStrip;
k.ForeColor = Color.Red; // as an example.
}
}https://stackoverflow.com/questions/12670439
复制相似问题