Rocky
Rocky

Reputation: 123

Displaying user input from a textbox into a label

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

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

        }

    }
}

Upvotes: 1

Views: 44950

Answers (4)

Dan Teesdale
Dan Teesdale

Reputation: 1863

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:

 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 another event.

Upvotes: 3

Ria
Ria

Reputation: 10347

try this:

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

Upvotes: 0

Mark Hall
Mark Hall

Reputation: 54532

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

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.

Upvotes: 0

Eric J.
Eric J.

Reputation: 150108

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 label1 and label2.

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 textBox1 at the point in time when the TextChanged event fires. The general form is

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 2 0 0 1. 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.

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

Upvotes: 2

Related Questions