Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not allow Enter Key to make a new line in MultiLine TextBox C#

I set the property Multiline=true;.

I do not want to allow the Enter Key to make a new line.

How can I solve this issue?

like image 519
user3618909 Avatar asked Feb 12 '23 02:02

user3618909


1 Answers

That could be as simple as listening to TextChanged event and then executing the following line inside it:

txtYourTextBox.Text = txtYourTextBox.Text.Replace(Environment.NewLine, "");

This solution involves some screen flicker though. A better way is to prevent it entirely by listening to KeyDown event:

private void txtYourTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
        e.SuppressKeyPress=true;
}
like image 161
dotNET Avatar answered Feb 14 '23 16:02

dotNET