I'm new to the forum, and fairly new to C# developing, and I'm needing a little assistance.
I have a form that has a series of textboxes. When I get to the last textbox, I want the user to be able to Tab or Enter and have it add the results to my XML file.
KeyPreview for the form is set to true, and below is the current coding I have to try and test the keystrokes:
//this is a textbox field
private void numbrackets_KeyDown(object sender, KeyEventArgs e)
{ if (e.KeyCode == Keys.Enter)
{
this.Close(); MessageBox.Show("Enter Was Pressed");e.Handled =
true;}
}
However, when I press Enter, I get nothing but the "beep" from the keyboard. Obviously I'm missing something, but for the life of me I can't figure out what. Any help would be great!
Thanks
Shawn
Mike GoldPosted May 31, 2007, 5:53 PM
The problem goes way back to original Windows Design (3.1 and on) and the operating system. Microsoft doesn't treat Enter and Tabs as regular keys because they act as system commands. For example, enter is used to press the OK button. Tab is used to traverse items in the dialog. Unfortunately, if you want to override this behavior you need to intercept the message coming into the window and tell it to ignore it. Microsoft made this a little bit easier in .NET by separating out system command with the override you are using.
Shawn PettyPosted May 31, 2007, 1:23 PM
Here's what I actually ended up using: (still allows me to tab through the other boxes, the one you gave me disabled the tabbing)
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter || keyData == Keys.Tab){
if (numbrackets.Focused){
MessageBox.Show("Enter or Tab was Pressed"); return true;}
else{
return false;}
}
return base.ProcessCmdKey(ref msg, keyData);}
Thanks alot for the help on this! Any reason why what I had before would fail? Everything I looked up seemed to point to doing it my way. This way works excellent however. Just trying to understand why it works compared to the one that isn't.
Mike GoldPosted May 31, 2007, 12:27 PM
Hi Shawn,
You need to see if the current edit box has focus in this command:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
if (textBox2.Focused)
{
MessageBox.Show("Enter was Pressed");
}
return true;
}
if (keyData == Keys.Tab){
if (textBox2.Focused)
{
textBox2.Text = Convert.ToString(Convert.ToInt32(textBox1.Text)*10);
} return base.ProcessCmdKey(ref msg, keyData);
}
return base.ProcessCmdKey(ref msg, keyData);
}
Shawn PettyPosted May 31, 2007, 12:18 PM
I'm trying to get it to track the keystrokes in my 3rd and final text box, not all three of them. Essentially I want the user to be able to tab field1, tab field2, and then when tabbing or pressing enter on the 3rd textbox, it will run the add record button.
Mike GoldPosted May 31, 2007, 12:13 PM
Try:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
MessageBox.Show("Enter Was Pressed");
return true;
} return base.ProcessCmdKey(ref msg, keyData);
}