Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change text of Label while typing in TextBox

Tags:

c#

winforms

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

like image 435
owebindex Avatar asked Jan 07 '23 02:01

owebindex


1 Answers

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.

like image 131
RomCoo Avatar answered Jan 08 '23 14:01

RomCoo