Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AcceptsReturn on TextBox not functioning correctly

Tags:

c#

.net

winforms

I have a simple search field on a form that is set as multiline (which I understand is the only way to change a text box's height) and has the flag AcceptsReturn set to false.

However, when I press enter within that control, instead of it activating the default button as it should, it puts in a return character.

Now, I've also attempted using the KeyPress event to check if the Enter key has been pressed to activate the search-button click function in the hope that it would override this return behaviour - but it hasn't. Now it just runs the search AND inserts a return character.

I'm running Visual Studio 2010 (although this problem seemed to be present in 2008 too before I converted it) and C# .NET 2.0. Any solutions?

like image 259
Chris Watts Avatar asked Jan 17 '23 17:01

Chris Watts


1 Answers

I see that an answer has already been posted, which mentions the AcceptButton property, but I figure I would state more clearly why that's necessary: quoth MSDN, on AcceptsReturn, "If there is no default button for the form, the ENTER key will always create a new line of text in the control, regardless of the value of this property." (I just tried it out on a dummy form - by "default button", they did in fact mean the form's AcceptButton property. With one set, the value of AcceptsReturn made a difference; without one, it had no effect.)

As for KeyPress, while that is obviously not the best way in this case, I have had to use tricks like that in the past - did you remember to set e.Handled to true in the case that you handled the event yourself?

like image 59
neminem Avatar answered Jan 28 '23 10:01

neminem