如何防止Enter键在多行文本框中添加新行?

How to prevent the enter key from making a new line in a multiple line text box?
2021-02-23
  •  译文(汉语)
  •  原文(英语)

我想防止Enter键使用KeyDown事件在多行文本框中创建新行.尽管使用textBox.Text =""或textBox.Clear()清除了textBox,但它的行为如下:http ://i.imgur.com/3sknAbO.png

private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
        {
            listBox1.Items.Add(textBox2.Text);
            textBox2.Text = "";
        }
}
速聊1:
您确定要阻止Enter键吗?如果用户粘贴一些包含换行符的文本怎么办?
速聊2:
我同意CodeCaster的意见,如果您只想占一行,请使用一行textbox,否则添加另一个按钮,以供该人员按提交textbox.
速聊3:
我加入了以前的评论员,但是如果您确实需要这样做,则可以使用KeyUp处理程序,并在删除文本后添加e.Handled = true;.
速聊4:
要处理粘贴的文本,请使用TextBox.TextChanged事件并清除换行符.
解决过程1

因此,基本上您想将一个文本框定义为多行,并且只接受其中的一长行?

好像您只想要一个单行文本框.将您的文本框定义为单行,并在设计时将其设置为看起来像多行文本框.

另一种选择是保留该框的原样,并允许用户使用其中的换行符输入该框.然后,当您需要数据时,只需在事实之后删除所有换行符.

解决过程2

我做了一些实验,似乎在KeyUp事件中添加了Enter键的新行.因此,这应该可以工作(请注意,我使用KeyUp而不是KeyDown):

private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        listBox1.Items.Add(textBox2.Text);
        textBox2.Text = "";
        e.Handled = true;
    }
}

或者您可以使用

textBox2.Select(0, 0);

而不是使用线 e.Handled = true;

I want to prevent the Enter key from making a new line inside the multiple line text box using the KeyDown event. Despite clearing the textBox using textBox.Text = "" or textBox.Clear(), it behaves like this: http://i.imgur.com/3sknAbO.png

private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
        {
            listBox1.Items.Add(textBox2.Text);
            textBox2.Text = "";
        }
}
Talk1:
Are you sure you want to prevent the Enter key? What if a user pastes some text containing newlines?
Talk2:
I agree with CodeCaster, if you only want to take one line then use a single line textbox, otherwise add another button for the person to press to submit the textbox.
Talk3:
I join previous commentators, but if you really need to do it that way, then you use KeyUp handler and after erasing text add e.Handled = true;.
Talk4:
To handle the pasted text, use the TextBox.TextChanged event and erase the newline character.
Solutions1

So, basically you want to define a text box as multi-line and only accept one long line from it?

Seems as though you only want a single line text box. Define your text box as single line and set it up at design time to look like a multi-line text box.

Another option would be to leave the box as-is and allow the user to enter it with the newline characters in it. Then, when you need the data, simply remove all newline characters after the fact.

Solutions2

I was experimenting a bit and it seems like the new line from the Enter key is added at the KeyUp event. So this should work (note that I use KeyUp instead of KeyDown):

private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        listBox1.Items.Add(textBox2.Text);
        textBox2.Text = "";
        e.Handled = true;
    }
}

Or you could set the cursor position with

textBox2.Select(0, 0);

instead of using the line e.Handled = true;

转载于:https://stackoverflow.com/questions/23712897/how-to-prevent-the-enter-key-from-making-a-new-line-in-a-multiple-line-text-box

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入