owebindex
owebindex

Reputation: 53

How to change text of Label while typing in TextBox

Please I'm new to C#, I created a textBox and a label. What i am expecting is, if I type a value into the textBox, I want it to display on the label and if I change the value it should also change immediately on the label. it work with the code below and i press enter key

 private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            label1.Text = textBox1.Text;
        }
   }

But I want it without press Enter/Return Key on keyboard.

Thanks for understanding

Upvotes: 4

Views: 7786

Answers (3)

RomCoo
RomCoo

Reputation: 1893

This works for VisualStudio

Select your TextBox in the Designer, go to the it's properties and click on the events (teh icon with the lightning). Then make a double click on the event that is called: TextChanged.

enter image description here

This creates a new function, that will always be called when the text of your TextBox changes. Insert following code into the function:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    TextBox tb = sender as TextBox;
    label1.Text = tb.Text;
}

That's it.

Upvotes: 7

Alexander Petrov
Alexander Petrov

Reputation: 14231

label.DataBindings.Add("Text", textBox, "Text");

Upvotes: 4

Eins
Eins

Reputation: 155

textbox KeyDown/Up/Press events may help. For example

private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            label1.Text += e.KeyData.ToString();
        }

Upvotes: 0

Related Questions