Original control is null when accessing sub-context element in C #
I am trying to change the color of a button when they click on a submenu item (colors> red) from the context menu bar.
This code is attached to a user-defined number of buttons. To figure out which button they are trying to change, I try to navigate from the item to the original item, for example: sender> owner tool strip> owner menu> source control.
My code:
private void redToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripItem subItem = sender as ToolStripItem;
if (subItem == null) return;
ToolStripItem mainItem = subItem.OwnerItem as ToolStripItem;
if (mainItem == null) return;
ContextMenuStrip menuStrip = mainItem.Owner as ContextMenuStrip;
if (menuStrip == null) return;
DataGridView dataGridView = menuStrip.SourceControl as DataGridView;
if (dataGridView == null) return; //null here
MessageBox.Show(dataGridView.Name);
}
From what I found on google it seems to be a bug. Are there any workarounds for this?
+3
source to share
No one has answered this question yet
See similar questions:
or similar: