MaskedTextBox + KeyEventArgs.SuppressKeyPress = bug?
-
My MaskedTextBox seems to have a bug in combination with the following function which gets called within the KeyDown event for all my TextBoxes:
private void dataEntryBehaviour(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down) { // e.KeyCode = Keys.Down; //to avoid the 1st input character disappearing bug //NOT ALLOWED - READ ONLY! //SuppressKeyPress is used to keep ENTER from being heard e.SuppressKeyPress = true; //this also sets e.Handled to true this.SelectNextControl((Control)sender, true, true, true, true); } else if (e.KeyCode == Keys.Up) { e.SuppressKeyPress = true; //this also sets e.Handled to true this.SelectNextControl((Control)sender, false, true, true, true); } }
The error is the following: After the user finishes entering the data, they can click a button to add the data into the database. The form then calls ResetText() on all the TextBoxes and the data entry begins anew. And here is where weird things happen: The first character typed into the MaskedTextBox disappears immediately after the second character input is typed (leaving a space behind). Also weird: If I don't set MaskedTextBox.SelectionStart = 1, the first character typed appears before what the Mask should allow ("(000) 000-0000"). Note that even if I set MaskedTextBox.SelectionStart = 1, which will display the first character to appear after the "(" literal, it'll still disappear after the second character is typed.Attempting to type "3056667777" results: Without setting MaskedTextBox.SelectionStart = 1, "(056) 667-777_" With setting MaskedTextBox.SelectionStart = 1, "(_05) 666-7777" So, essentially, to get the results I want I'd have to not set MaskedTextBox.SelectionStart = 1, and type "33056667777".
Now, this error only occurs on in my MaskedTextBoxes (not my vanilla TextBoxes) and only when ENTER is pressed to navigate away from that particular control. Also, if I use KeyEventArgs.Handled instead KeyEventArgs.SuppressKeyPress - the problem disappears. Unfortunately, I then get a "Windows XP Ding" every time I hit ENTER. What am I doing wrong or is this just a bug with MS' new control?Alex