ContextMenuStrip.Owner属性null从嵌套ToolStripMenuItem检索时
我有一个带有两个ToolStripItem
的ContextMenuStrip
设置。 第二个ToolStripItem
有两个额外的嵌套ToolStripItem
。 我将其定义为:
ContextMenuStrip cms = new ContextMenuStrip(); ToolStripMenuItem contextJumpTo = new ToolStripMenuItem(); ToolStripMenuItem contextJumpToHeatmap = new ToolStripMenuItem(); ToolStripMenuItem contextJumpToHeatmapStart = new ToolStripMenuItem(); ToolStripMenuItem contextJumpToHeatmapLast = new ToolStripMenuItem(); cms.Items.AddRange(new ToolStripItem[] { contextJumpTo, contextJumpToHeatmap}); cms.Size = new System.Drawing.Size(176, 148); contextJumpTo.Size = new System.Drawing.Size(175, 22); contextJumpTo.Text = "Jump To (No Heatmapping)"; contextJumpToHeatmap.Size = new System.Drawing.Size(175, 22); contextJumpToHeatmap.Text = "Jump To (With Heatmapping)"; contextJumpToHeatmap.DropDownItems.AddRange(new ToolStripItem[] { contextJumpToHeatmapStart, contextJumpToHeatmapLast }); contextJumpToHeatmapStart.Size = new System.Drawing.Size(165, 22); contextJumpToHeatmapStart.Text = "From Start of File"; contextJumpToHeatmapLast.Size = new System.Drawing.Size(165, 22); contextJumpToHeatmapLast.Text = "From Last Data Point";
然后,我为我想要响应的三个ToolStripMenuItem
的click事件设置一个事件监听器。 以下是方法(我只列出了三种方法中的两种):
void contextJumpTo_Click(object sender, EventArgs e) { // Try to cast the sender to a ToolStripItem ToolStripMenuItem menuItem = sender as ToolStripMenuItem; if (menuItem != null) { // Retrieve the ContextMenuStrip that owns this ToolStripItem ContextMenuStrip owner = menuItem.Owner as ContextMenuStrip; if (owner != null) { // Get the control that is displaying this context menu DataGridView dgv = owner.SourceControl as DataGridView; if (dgv != null) // DO WORK } } } void contextJumpToHeatmapStart_Click(object sender, EventArgs e) { // Try to cast the sender to a ToolStripItem ToolStripMenuItem menuItem = sender as ToolStripMenuItem; if (menuItem != null) { // Retrieve the ToolStripItem that owns this ToolStripItem ToolStripMenuItem ownerItem = menuItem.OwnerItem as ToolStripMenuItem; if (ownerItem != null) { // Retrieve the ContextMenuStrip that owns this ToolStripItem ContextMenuStrip owner = ownerItem.Owner as ContextMenuStrip; if (owner != null) { // Get the control that is displaying this context menu DataGridView dgv = owner.SourceControl as DataGridView; if (dgv != null) // DO WORK } } } }
这是我的问题:
我的contextJumpTo_Click
方法工作得很好。 我们一直到我确定点击来自哪个DataGridView
的地方,我可以继续。 但是, contextJumpTo
ToolStripMenuItem
不是ContextMenuStrip
上的嵌套菜单项。
但我对contextJumpToHeatmapStart_Click
方法不能正常工作。 当我到达我确定owner.SourceControl
, SourceControl
为null,我无法继续。 现在我知道这个ToolStripMenuItem
嵌套在我的ContextMenuStrip
另一个下,但是为什么我的ContextMenuStrip
上的SourceControl
属性突然为null?
如何获取ContextMenuStrip
的嵌套ToolStripMenuItem
的SourceControl
?
我相信这是一个错误。
我试图抓取工具条父项列表以获取ContextStripMenu所有者,该工作正常,但SourceControl属性始终为null。
看起来常见的工作是在上下文菜单的开头设置控件:
private Control menuSource; cms.Opening += cms_Opening; void cms_Opening(object sender, CancelEventArgs e) { menuSource = ((ContextMenuStrip)sender).SourceControl; }
然后你的代码基本上变成了这个:
上述就是C#学习教程:ContextMenuStrip.Owner属性null从嵌套ToolStripMenuItem检索时分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
DataGridView dgv = menuSource as DataGridView; if (dgv != null) { // do work }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1002347.html