Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context Menu Strip -> Changing the color of Highlighted Items

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.

like image 490
e e Avatar asked Dec 26 '22 18:12

e e


1 Answers

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.

like image 63
Gareth Avatar answered Dec 29 '22 10:12

Gareth