So I'm having a bit of a problem handling a situation. I have an application, which is an MDI application, which opens custom documents. My MDI parent has a ToolStrip, with a few controls, a text box and a button. The text box holds a qty value for an item, then there is a button to add the item to the document. the model for fastest operation is, starting with the qty text box, enter the qty, tab to the part number text box and enter the part #, then tab to the add button and hit either enter key or space key. This is way faster than using the mouse to click the button, or click to enter the text box. So I want my app to auto focus the qty text box once the button is fired.
I've handled the ToolStripMenuItem.Click event, which adds the item, and then refocuses the qty box, and it works perfectly when the mouse is used to click the button. But for some reason I've yet to discover, this does NOT work for when the keyboard triggers the button. I know the click event is being triggered, because the item gets added properly, but the focus is not handled correctly. In fact, in this case, it seems the focus is just lost completely, tab ceases to function, focus can't be regained except with the mouse. I've tried the following:
Call Form.Activate() i the parent form, and handle the Activated event to focus the text box.
Call Focus() on the parent form, and handle the GotFocus event to focus the text box.
It's as if the underlying framework fails to properly handle key events, and there are no Key events exposed for the ToolStripMenuItemButton control. I'm sort of at a loss. The app is functional, as the mouse works, and the app can actually be used, but for best functionality I need to get this working so that fast data entry can be done without the use of the mouse.
You're right, that is a weird behavior. I was able to overcome it like this:
public partial class Form1 : Form
{
bool myButtonJustClicked = false;
public Form1()
{
InitializeComponent();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
myButtonJustClicked = true;
toolStripTextBox1.Focus();
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if(myButtonJustClicked)
{
toolStripTextBox1.Focus();
myButtonJustClicked = false;
}
}
}
Basically, I'm using the form's "KeyUp" event to handle the KeyUp event that should be fired on the button but there is no "KeyUp" event, or any keyboard-related events, for some reason on ToolStripButtons.
In order to make sure that the textbox doesn't get the focus when other KeyUp events fire elsewhere in the form, I use the myButtonJustClicked boolean as shown.
Also, I left the toolStripTextBox1.Focus();
in the toolStripButton1_Click
method to handle the mouse click, of course.