Groups | Blog | Home
all groups > c# > january 2004 >

c# : TextBox.AcceptsReturn Property


Chuck Bowling
1/30/2004 11:48:16 PM
Am I doing something wrong or does the TextBox.AcceptsReturn property not
work as documented?

According to the help, "true if the ENTER key creates a new line of text in
a multiline version of the control; false if the ENTER key activates the
default button for the form."

I don't want the Enter key to create a new line in the textbox. I have the
property set to false but it makes a newline when I type in it anyway...
What's up with that?

C# Learner
1/31/2004 4:51:30 AM
[quoted text, click to view]

From
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformstextboxclassacceptsreturntopic.asp:

"If the value of this property is false, the user must press
CTRL+ENTER to create a new line in a multiline TextBox control. If
there is no default button for the form, then the ENTER key will
always create a new line of text in the control, no matter what the
value of this property."

C# Learner
1/31/2004 4:58:07 AM
[quoted text, click to view]

<snip>

If you don't want the user to be able to insert a new line in a
multi-line text box, and you don't have a default button on your form,
you could do it like this:

private void textBox_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = e.KeyChar == 13;
Chuck Bowling
1/31/2004 12:21:21 PM

[quoted text, click to view]

Ah... I scoured MSDN but I missed this... thanks...

Chuck Bowling
1/31/2004 12:25:44 PM

[quoted text, click to view]

Actually what I wanted to do was implement a different behavior than the
default but I don't want to add a default button to my UserControl to do it.

I solved the problem by creating a control inherited from TextBox and
overriding ProcessCmdKey.

Thanks for your help.

AddThis Social Bookmark Button