如何使用enter键将数据从文本框传输到标签

时间:2012-07-15 03:05:28

标签: c# textbox label

我对一个小项目提出了一个快速而简单的问题,我在C#中开始使用Visual Studio 2010进行Windows窗体程序。我似乎找不到正确的代码来传输输入数据用户使用按Enter键的方法进入文本框,并自动在同一表单上的该标签中输入消息。

例如在下面的代码中(已经提供了建议编辑):

    namespace MovieFinders2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void label1_Click(object sender, EventArgs e)
            {
                //Named "Enter a Year"
            }

            private void textBox1_TextChanged(object sender, EventArgs e)
            {

            }

            void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
                {
                    label2.Text = textBox1.Text;
                    label2.Text = "Movies released before " + textBox1.Text;
                }
            } 

            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {

            }

            private void label2_Click(object sender, EventArgs e)
            {
                label2.Text = textBox1.Text;
                label2.Text = "Movies released before " + textBox1.Text;
            }

            private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
            {

            }

            private void label3_Click(object sender, EventArgs e)
            {
                label3.Text = textBox1.Text;
                label3.Text = "Movies released in or after " + textBox1.Text;
            }
        }
    }

            private void label3_Click(object sender, EventArgs e)
            {
                label3.Text = textBox1.Text;
                label3.Text = "Movies released in or after " + textBox1.Text;
            }
        }
    }

我知道这个程序还处于早期阶段,但我一次尝试这一步,这是我在这一点上所遇到的障碍;所以任何和所有的帮助都将非常感激现在,当我在标签上单击鼠标时,它会在该标签中显示该消息。当用户按下回车键时,我需要在标签中显示该信息。

1 个答案:

答案 0 :(得分:1)

试试这个:

void textBox1_KeyDown(object sender, KeyEventArgs e) {
    if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) {
        label2.Text = textBox1.Text;
        label2.Text = "Movies released before " + textBox1.Text;
    }
}

TextBox.KeyDown事件