如何在文本框中使用Enter键 [英] how to use enter key in a text box

查看:121
本文介绍了如何在文本框中使用Enter键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框.
当我在第一个文本框中输入内容并按Enter键后,文本将发送到第二个文本框,这意味着第一个文本框的Enter键就像按钮控件一样工作.
我正在使用此代码,但它给出了错误-

Hi i have two text boxs.
When i am write something in first text box and press enter key then text is send to second text box Means enter key of first text box is work like a button control.
i am using this code but it gives error-

private void txtwriteinenglish_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode = Keys.Enter}
    {
    }
}



在此先感谢.



Thanks in Advance.

推荐答案

Srivatsava

通常,keydown事件很适合处理Enter键.
您能告诉我使用keydown事件时遇到的错误吗?

文本框已清除,但光标从第二行开始"

按下键时,它将传递到文本框.
要在KeyDown事件集中抑制此行为,

Hi Srivatsava

The keydown event is generally good to handle the Enter key.
Can you please tell what error you are getting when keydown event is used.

"text box is clear but cursor is start from second line"

When a key is pressed it is passed on to the text box.
To suppress this behavior in KeyDown event set

e.SuppressKeyPress = True


并在KeyPress事件集中设置


and in KeyPress event set

e.Handled = true



这样就不会将密钥传递给文本框



so that the key is not passed on to the text box


private void txtwriteinenglish_KeyPress(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == (char)13}
        {
            txtSecondTextBox.Text = txtwriteinenglish.Text.ToString();
            txtwriteinenglish.Text = string.Empty;
            txtwriteinenglish.Focus();
            return;
        }
    }



这段代码将用作您第一个文本框的按钮



This Piece Of Code Will Work As A Button For Your First TextBox


Hi, when i press enter in a txtwriteenglish.Text then text is goes on second text it works fine but then i want to clear txtwriteenglish.Text.The text box is clear but cursor is start from second line of txtwriteenglish.Text.



该问题的解决方案是:



The Solution For This Question Is:

private void txtwriteinenglish_KeyPress(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == (char)13}
        {
            txtSecondTextBox.Text = txtwriteinenglish.Text.ToString();
            txtwriteinenglish.Text = string.Empty;
            txtwriteinenglish.Select(0, 0);//To Set The Cursor To Default Position
            txtwriteinenglish.Focus();
            return;
        }
    }


这篇关于如何在文本框中使用Enter键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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