ecfedele
ecfedele

Reputation: 316

Auto-hiding MenuStrip

I'm working on a Windows Forms project, and I recently added code to keep the MenuStrip hidden by default unless the user presses the Alt key, in keeping with recent versions of Windows' visual practices.

What I'm looking to do is complete this addition by enabling the MenuStrip to auto-hide if activity is not detected in a certain timeframe. Without further ado, here's the code I've drafted so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SomeWinformProject {
    public partial class MainForm : Form {
        /* Construction */
        public MainForm () {
            InitializeComponent ();
            this.KeyDown += new KeyEventHandler(this.MainForm_KeyDown);
            this.KeyPreview = true;
            this.menuStrip.Visible = false;
        }

        /* Alt key event handler */
        private void MainForm_KeyDown (object sender, KeyEventArgs e) {
            /* if the Alt key is pressed and the menuStrip is not currently
             * visible, un-hide it */
             if (e.Alt && !(this.menuStrip.Visible))
                 this.menuStrip.Visible = true;
        }
    }
}

Here's what I've thought of doing to work around this problem myself:

  1. Create a public Boolean called something to the effect of menuStripActivity and have it set to false in MainForm_KeyDown(). Then, I was thinking of creating a timer instance on an interval of 5 seconds or so, and also attaching a MouseClick event to menuStrip. If the MouseClick event occurred, then menuStripActivity would be set to true, and the timer interrupt would elect to do nothing, rather than hiding the MenuStrip.
  2. The issue I've realized exists with the above is that it doesn't account for the possibility that the user is in the process of doing something, or navigating the menuStrip options. I read in the MSDN docs that a MouseHover event handler also exists, in which case I could OR the results of the two handlers when the timer trip occurs.

The issue with both of these is that I am fairly new to C# and the .NET ecosystem at large, so I don't have a good idea of what is proper and what is kludgy. I want to avoid my code being cluttered from the get-go and want to err on the side of "best practice" for this type of situation.

Can anyone help point me in the right direction (or show me errors that I'm making)?

Upvotes: 2

Views: 4028

Answers (1)

Xiaoy312
Xiaoy312

Reputation: 14477

You can listen to the MenuStrip.MenuDeactivate event:

public MainForm () {
    InitializeComponent ();

    this.KeyPreview = true;
    this.KeyDown += new KeyEventHandler(this.MainForm_KeyDown);
    this.menuStrip.MenuDeactivate += (s, e) => this.menuStrip.Visible = false;

    this.menuStrip.Visible = false;
}

Remarks
When activated by the ALT key, the MenuStrip or ToolStrip typically neither take nor remove the focus from the control that currently has the focus. If there is a control hosted within the MenuStrip or a drop-down of the MenuStrip, the control gains focus when the user presses the TAB key. In general, the GotFocus, LostFocus, Enter, and Leave events of MenuStrip might not be raised when they are activated by the keyboard. In such cases, use the MenuActivate and MenuDeactivate events instead.

Upvotes: 2

Related Questions