So on my Context Menu Strip, I want to change the color of selected item. For example, I Want Menu Item "About Me" to change color from White to Black when it has been highlighted. This is What I been trying:
ContextMenuStrip CMS = new ContextMenuStrip();
CMS.ForeColor = Color.White;
CMS.ShowImageMargin = false;
var item = CMS.Items.Add("About Me", null);
item.MouseHover += new EventHandler(CMS_MouseHover);
void CMS_MouseHover(object sender, EventArgs e)
{
ContextMenuStrip CMS = sender as ContextMenuStrip;
CMS.ForeColor = Color.Black;
}
I also have tried playing around with the ToolStripMenuitem event handlers, but I don't think it will be applied here ?
Any suggestion on how I could accomplish this ?
I was also Wondering if there was a way to change the highlight color, I know it depends on your system but I was just wondering.
I'm not convinced this is the best way, but one possible way to do this is to wire up the MouseEnter and MouseLeave events on the individual ToolStripMenuItems on your ContextMenuStrip.
For example:
private void aboutToolStripMenuItem_MouseEnter(object sender, EventArgs e)
{
ToolStripMenuItem TSMI = sender as ToolStripMenuItem;
TSMI.ForeColor = Color.Black;
}
private void aboutToolStripMenuItem_MouseLeave(object sender, EventArgs e)
{
ToolStripMenuItem TSMI = sender as ToolStripMenuItem;
TSMI.ForeColor = Color.White;
}
Obviously, you also need to also wire up the event handlers on your programatically created ToolStripMenuItem.
You seem to be trying to change the ForeColor of the whole ContextMenuStrip with "CMS.ForeColor = Color.Black", which isn't what you said you wanted. Tried the above and it does work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With