I am trying to change the color of a button when they click on a sub menu item (colors > red) from a Context Menu Strip.
This code is attached to user defined amount of buttons. To figure out which button they are trying to change, I am trying to go from the sub item to the source control like such: sender > owner tool strip > owner menu > source control.
My Code:
private void redToolStripMenuItem_Click(object sender, EventArgs e)
{
var subItem = sender as ToolStripItem;
if (subItem == null)
return;
var mainItem = subItem.OwnerItem as ToolStripItem;
if (mainItem == null)
return;
var menuStrip = mainItem.Owner as ContextMenuStrip;
if (menuStrip == null)
return;
var dataGridView = menuStrip.SourceControl as DataGridView;
if (dataGridView == null)
return; //null here
MessageBox.Show(dataGridView.Name);
}
From what I've found on google, this appears to be a bug. Are there any workarounds for this?