Check if a menustrip is highlighted/selected

leorob88

Active member
Joined
Dec 31, 2020
Messages
39
Programming Experience
1-3
I'm handling colors for menustrips, so, since i have a black list with white text, when an item is selected it turns azure-ish. My problem would be to read the text inside, since it's white.
While you can solve this using Mouse_Enter and Mouse_Leave, this problem remains when you get to an adjacent item, like a submenu or to a previous menu but not selecting anything different. Like, imagine having a classic Windows list,

Edit ->
----Cut
----Copy
----Paste
----Find ->
--------Text
--------Image
--------Etc.

If you select Text, Find is still selected. Similarly, if you select again Find, Text still remains selected unless you select Cut, Copy or Paste.
So, in these cases the Mouse events are useless, since the menu option stays selected even though the mouse isn't there anymore. So, in my case, using Mouse_Enter to get a dark text and Mouse_Leave to get back a clear text, would work while navigating in the same submenu (meaning, Cut, Copy, etc.) but not when it comes to multiple selections.
Do you know a way to state something like "while/when this menu item is selected (not while the mouse is hovering) change the forecolor"?
I could make something "dumb" like creating an array/list containing the names of "menus that are open right now" so anytime i select something i would refresh it and change the color for those in the list, still it seems a "dumb" idea to me and i'd rather do something just smarter...
 
Last edited:
It's always good to look at the inheritance chain of the class you are dealing with. MS is inconsistent in its documentation. Sometimes the child class' documentation list all the ancestors' methods, properties, and events; other times they don't.
 
It's always good to look at the inheritance chain of the class you are dealing with. MS is inconsistent in its documentation. Sometimes the child class' documentation list all the ancestors' methods, properties, and events; other times they don't.
Still, there's another issue I'm having....the selected property works fine while navigating the menus. on mouse enter/leave i scripted a check to set the forecolor for every menu item (if selected, forecolor black, else white). but if the mouse moves out on the form, this doesn't work properly, as it turns white even if it is selected. i'm trying to find some workaround for this. i find it difficult to script "if the mouse goes off on the form". i tried with Form1_MouseMove/Enter but nothing changes...
 
See my post in another thread where I have code that checks for mouse position being in the client area.

 
As quick note: in C#, code is called "code" rather than "script". It's only when you are writing C# code in Unity when it is called "script".
 
See my post in another thread where I have code that checks for mouse position being in the client area.

I found another solution. I posted earlier another answer with some questions about your code, but in the end i found a "simple" solution (not elegant maybe as yours, but still it's cheap and works).
C#:
//inside partial class Form1 : Form
//also, remember i use my method to check when i'm on dark theme form, you can use what you prefer for this

bool mousetransition = false;

public Form1()

        {

            InitializeComponent();
            //Now here's what I added here
            foreach (ToolStripMenuItem Tm_items in barramenu.Items) //barramenu is the overall menu bar
            {
                Tm_items.DropDownOpened += (sender, args) => {
                    if (cambiatema.Text == "Tema scuro → chiaro") //basically, when i'm on dark theme
                    {
                        timer2.Enabled = true; //below timer code starts
                        guidaToolStripMenuItem.ForeColor = SystemColors.ControlText; //***this mixes with other code below
                    }
                };
                Tm_items.DropDownClosed += (sender, args) => {
                    if (cambiatema.Text == "Tema scuro → chiaro")
                    {
                        timer2.Enabled = false; //below timer code stops
                        if (!mousetransition)
                        {
                            //set main header forecolor to white if you close the menu clicking outside
                            guidaToolStripMenuItem.ForeColor = Color.FromArgb(220, 220, 220);
                        }
                    }
                };
            }
        }


        private void timer2_Tick(object sender, EventArgs e)
        {
            //here basically set selected menustrips forecolor black, the others white
            //to do this i have a list with all the menustrips, i use a "for" loop with it
            //so, if menustrip[i].selected -> forecolor black, else forecolor white
        
            //then the main "header"/container still stays forecolor black since it has white backcolor
            guidaToolStripMenuItem.ForeColor = SystemColors.ControlText;
        }
        //these 2 handlers are used for the main container guidaToolStripMenuItem
        private void darkmenu_MouseEnter(object sender, EventArgs e)
        {
            //use your "if" to check when it's dark theme
            guidaToolStripMenuItem.ForeColor = SystemColors.ControlText;
            mousetransition = true;
        }
        private void darkmenu_MouseLeave(object sender, EventArgs e)
        {
            //use your "if" to check when it's dark theme
            guidaToolStripMenuItem.ForeColor = Color.FromArgb(220, 220, 220);
            mousetransition = false;
        }

And that's it. Now based on when the menu opens and closes and whether it closes by clicking on the "header" or on the form, it gets colored as i need. Selected options have always dark text and viceversa. Everytime something is not selected anymore it turns to the right forecolor.
 
Last edited:
Back
Top Bottom