ContextMenuStrip.Owner property null When retrieving from a nested ToolStripMenuItem

I have a ContextMenuStrip setup with two ToolStripItem s. The second ToolStripItem has two additional nested ToolStripItem s. I define this as:

 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"; 

Then I set up an event listener for the click events of the three ToolStripMenuItem that I want to respond to. Here are the methods (I listed only two of the three methods):

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

Here is my problem:

My contextJumpTo_Click method works fine. We get to the place where I determine which DataGridView the click came from, and I can continue. contextJumpTo ToolStripMenuItem , however, is NOT a nested menu item on ContextMenuStrip .

But my method for contextJumpToHeatmapStart_Click does not work correctly. When I go to the line where I define owner.SourceControl , SourceControl is null and I cannot continue. Now I know that this ToolStripMenuItem nested under another in my ContextMenuStrip , but why is the SourceControl property suddently null on my ContextMenuStrip ?

How to get SourceControl for nested ToolStripMenuItem for ContextMenuStrip ?

+8
c # winforms contextmenustrip
source share
1 answer

I believe that a mistake.

I tried to scan the list of parent panels to get to the owner of ContextStripMenu, which worked, but the SourceControl property was always null.

It seems like the overall work is to set the control when opening the context menu:

 private Control menuSource; cms.Opening += cms_Opening; void cms_Opening(object sender, CancelEventArgs e) { menuSource = ((ContextMenuStrip)sender).SourceControl; } 

Then your code basically turns into this:

 DataGridView dgv = menuSource as DataGridView; if (dgv != null) { // do work } 
+8
source share

All Articles