I have a textbox on a form where I’m trying to detect the keys the user types in. The TextBox is multilined with wordwrap on. I don’t want the user the press the enter key (as I want all text entered on ONE line, wrapped) so I used the following code:
private void txtPlain_KeyPress(object sender, KeyPressEventArgs e) {
if (e.KeyChar == (char)13) {
MessageBox.Show("Enter keys are not allowed");
e.KeyChar = (char)0;
}
}
This worked fine in my tests, but when I tested for CTRL+ENTER it didn’t work as I’m not sure how to detect for the control key. From my googling I found that I need to use the KeyUp/Down events so I now have the following Code:
private void txtPlain_KeyUp(object sender, KeyEventArgs e) {
//if (e.KeyData == (Keys.Control | Keys.Enter)) {
if (e.KeyCode == Keys.Enter || (e.KeyCode == Keys.Enter && e.Control)) {
MessageBox.Show("Enter keys are not allowed:");
//e.KeyValue = Keys.None;
}
}
The first commented out line didn’t work for some reason so if anyone could explain why this would be useful.
The problem with the KeyUp/Down event is that I don’t know how to REMOVE the enter key from the text – unlike the KeyPress event when I can set the KeyChar to zero. The event captures both the Enter and Ctrl+Enter keys, but the cursor still goes to the next line in the TextBox.
Thanks for any help on this.
Hmm, there’s no reason to disallow the Enter key by handling the
KeyDownorKeyUpevents. You can simply set theAcceptsReturnproperty of the textbox control to False. This will prevent a multiline textbox from responding to a press of the Enter key.Of course, this doesn’t solve the problem of Ctrl+Enter. In fact, that’s the expected way to create a new line when the
AcceptsReturnproperty is set to False. To solve that, you will need to handle one of the keyboard events and prevent the control from receiving this input.KeyDownis a good place to start. What you want to do is filter out any keyboard events that include theKeys.Enterflag. That will catch them no matter which other modifier key they might be combined with. Then, once you’ve found an Enter keypress, you want to set thee.Handledproperty to True in order to prevent it from being passed on to the control.But unfortunately, we’re not quite done yet. The textbox control tries to handle certain keys internally, and you’re not going to be able to override that in a key event handler method. You also need to tell the control not to interpret that particular key as an input key. There are two primary ways of doing this. The first (and recommended way) is to inherit from the base
TextBoxclass to create your own custom control, and then override the protectedIsInputKeymethod. The second (somewhat simpler) way is just to handle thePreviewKeyDownevent, and set theIsInputKeyproperty to False.Sample code:
And, though I assume this is for testing purposes only, you definitely want to take that
MessageBoxcall out of there for production code. Find another way to alert the user that their input was not allowed, such as a short beep sound and anErrorProvidercomponent placed next to the textbox. Showing a message box is very jarring, and not very user-friendly. See my answer here for other hints and tips.