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 ?
Michael mankus
source share