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

假装没事ソ 提交于 2019-12-23 19:25:41

问题


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?


回答1:


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;
}


来源:https://stackoverflow.com/questions/25700992/do-not-allow-enter-key-to-make-a-new-line-in-multiline-textbox-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!