Reputation: 4655
In order to use a ToolStripTextBox as menu item in practical programming I need clearance for some things which behaves different as I would expect.
I have menu strip with few menu strip items among which is one textbox item.
1) After usage of textbox terminated with key ENTER I would like to close a menustrip but I dont know how.
I can hide it:
myToolStripMenuItem.HideDropDown()
But it stay sensitive if I move mouse over and then opens automatically. I would like to close it that click would be needed for opening it again.
And second, If I activate menu with alt key and navigate down when selected item comes to that textbox it automatically throw this textbox in enter mode and such block keyboard navigation.
Is this normal behaviour and can this be avoided that (say) SPACE or ENTER would be need for turn a textbox in enter mode so I can pass with arrows (up/down) like over any other menu item?
Upvotes: 1
Views: 1285
Reputation: 942428
I see the problem. The issue is that the toolstrip manager still has the menu in the active state so a mouse hover is going to open the menu again instead of selecting it. Fixing this is very difficult, the methods are internal. You can hack it by using Reflection, like this:
...
using System.Reflection;
void box_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == Keys.Enter) {
e.Handled = e.SuppressKeyPress = true;
myToolStripMenuItem.HideDropDown();
var t = typeof(MenuStrip).Assembly.GetType("System.Windows.Forms.ToolStripManager+ModalMenuFilter", true);
var mi = t.GetMethod("ExitMenuMode", BindingFlags.Static | BindingFlags.NonPublic);
mi.Invoke(null, null);
}
}
I can't really recommend it. Nor would I recommend a text box in a menu. The UI hints are very poor, it just isn't very discoverable.
Upvotes: 3
Reputation: 2413
I wouldn't try to 'close' it. Instead I would use the following:
myToolStripMenuItem.Enabled = false;
myToolStripMenuItem.Visible = false;
Then you can dynamically re-enable it and make it visible later whether with a mouse event or key event.
Upvotes: 0