\n 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.
\n\n","author":{"@type":"Person","name":"Xiaoy312"},"upvoteCount":2}}}Reputation: 316
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:
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.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
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