views:

1410

answers:

2

How do I prevent the caret to go to the next line when the 'ENTER' key has being pressed on a text-box? In other words how to disable the 'ENTER' or 'RETURN' key on a text-box?

+5  A: 

You can write the OnKeyDown event. you can use the e.SuppressKeyPress to tell .net that you handle the key. something like this:

        if (e.KeyCode == Keys.Enter) {
            e.SuppressKeyPress = true;
        }
Jedi Master Spooky
+1 for useful information for the general case even if it's not the best approach for this particular scenario
Davy8
WOW fast reply... this is way better than other forums (don't want to say which ones) where people insult people who ask questions.Thanks, thanks.
KeyPressEventArgs doesn't contains SuppressKeyPress... I am using OnKeyPress event
If you want to suppress de key you would have to use OnKeyDown
Jedi Master Spooky
+5  A: 

Take a look at TextBox.AcceptsReturn.

Jakob Christensen
is there a way to do this on a richtextbox?
I am afraid not. But you can override OnKeyDown as Jedi Master Spooky suggests.
Jakob Christensen