不允许Enter键在MultiLine TextBox C#中换行 [英] Do not allow Enter Key to make a new line in MultiLine TextBox C#

查看:630
本文介绍了不允许Enter键在MultiLine TextBox C#中换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了属性Multiline=true;.

我不想让Enter键换行.

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

我该如何解决这个问题?

How can I solve this issue?

推荐答案

那可能就像侦听TextChanged事件然后在其中执行以下行一样简单:

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, "");

该解决方案虽然涉及一些屏幕闪烁.更好的方法是通过监听KeyDown事件来完全阻止它:

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

这篇关于不允许Enter键在MultiLine TextBox C#中换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆