C# 将用户输入从文本框显示到标签中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11480628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:00:14  来源:igfitidea点击:

Displaying user input from a textbox into a label

c#textboxlabels

提问by Rocky

I have a big project here that I am going through very slowly.

我在这里有一个大项目,我正在非常缓慢地进行。

The main objective of this project in C# for a Windows main form program through Visual Studio 2010, is that it will allow the user to enter in a year into a text box. When the user presses the enter key, a list of movies will appear in two seperate list boxes; these movies are sorted by the year that the user entered. The first list box will display a list of movies that are lower than what the user entered and the second list box will display a list of movies that are equal to and greater than the year that the user entered. There will also be two labels above the list boxes that will display the year that the user entered (amongst other words).

这个项目在 C# 中的主要目标是通过 Visual Studio 2010 为 Windows 主窗体程序,它允许用户在文本框中输入年份。当用户按下回车键时,电影列表将出现在两个单独的列表框中;这些电影按用户输入的年份排序。第一个列表框将显示低于用户输入的电影列表,第二个列表框将显示等于和大于用户输入的年份的电影列表。列表框上方还将有两个标签,用于显示用户输入的年份(以及其他字词)。

This is the first problem that I have right now; how do I get the data that the user entered into a text box to appear in two seperate labels? Any and all responses to this will be greatly appreciated.

这是我现在遇到的第一个问题;如何让用户在文本框中输入的数据显示在两个单独的标签中?对此的任何和所有回应都将不胜感激。

Right now I just have the basic shell of the coding written from the controls on the form; this is what I have so far to start out with:

现在我只有从表单上的控件编写的编码的基本外壳;这是我到目前为止的开始:

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)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        private void label3_Click(object sender, EventArgs e)
        {

        }

    }
}

回答by Eric J.

Since this is homework, I'll provide a nudge rather than a solution.

由于这是家庭作业,我将提供一个推动而不是解决方案。

Presumably you have two labels not shown in your code. I'll designate them label1and label2.

据推测,您的代码中没有显示两个标签。我会指定他们label1label2

When text in your textbox changes, the event handler that you have shown in your code will be triggered

当文本框中的文本更改时,将触发您在代码中显示的事件处理程序

textBox1_TextChanged

You could use the Text property of each of the two labels to set the text of the label, based on what is in textBox1at the point in time when the TextChanged event fires. The general form is

您可以使用两个标签中每一个的 Text 属性来设置标签的文本,这取决于textBox1TextChanged 事件触发时的内容。一般形式是

labelSome = "Some Text Here";

Note though that populating the listboxes the same way will lead to confusing behavior. If I want to type 2001, the TextChanged event will fire four times, once for each of 2001. The actual text will be, respectively, 2, 20, 200, 2001. If you start updating the list boxes for each event, the first event would have you divide movies between ones before and ones after the year 2. And so on.

请注意,尽管以相同的方式填充列表框会导致令人困惑的行为。如果我想输入 2001,TextChanged 事件将触发四次,每个2001. 实际文本将分别为2202002001。如果您开始更新每个事件的列表框,第一个事件会让您将电影分为 2 年之前和之后的电影。依此类推。

A better user interface would be to provide a Search button that both populates the listboxes and updates the text of the labels.

更好的用户界面是提供一个搜索按钮,它既可以填充列表框,又可以更新标签文本。

回答by Dan Teesdale

You should be able to access the controls in the code behind. Intellisense will generally list the controls as you type them and make the task pretty simple:

您应该能够访问后面代码中的控件。Intellisense 通常会在您键入控件时列出控件,并使任务变得非常简单:

 private void textBox1_TextChanged(object sender, EventArgs e)
 {
     Label1.Text = textBox1.Text;
     Label2.Text = textBox1.Text;
 }

Note: This probably should not be your final code. From reading your requirements, it seems like the labels should not be updated whenever the textbox changes, but rather during anotherevent.

注意:这可能不应该是您的最终代码。从阅读您的要求来看,标签似乎不应该在文本框更改时更新,而是在另一个事件期间更新。

回答by Mark Hall

Based on your comment to DanT. You can try using String.Format with Composite Formating

根据您对 DanT 的评论。您可以尝试将 String.Format 与复合格式一起使用

From Link:

来自链接:

A composite format string and object list are used as arguments of methods that support the composite formatting feature. A composite format string consists of zero or more runs of fixed text intermixed with one or more format items. The fixed text is any string that you choose, and each format item corresponds to an object or boxed structure in the list. The composite formatting feature returns a new result string where each format item is replaced by the string representation of the corresponding object in the list.

复合格式字符串和对象列表用作支持复合格式功能的方法的参数。复合格式字符串由零个或多个与一个或多个格式项混合的固定文本组成。固定文本是您选择的任何字符串,每个格式项对应于列表中的一个对象或装箱结构。复合格式功能返回一个新的结果字符串,其中每个格式项都被列表中相应对象的字符串表示替换。

回答by Ria

try this:

尝试这个:

textBox.TextChanged += (sender, e) =>
    {
        label2.Text = label1.Text = ((TextBox)sender).Text;
    };