Reputation: 53
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
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
.
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
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