How to dynamically update label text when changing a text field

I am having a problem updating the text of my label. I don’t know how to do this.

I have a label (lable1) and a text field (secondTextBox), and I have a tree view from which the user needs to select items. The process is as follows:

The user selects the item in the tree view, label1 displays the default text and a second TextBox appears. When the user changes the default text inside secondTextBox, the text inside label1 should be automatically updated without the user clicking anything (remember that I have about 45 nodes that need this to be active, is there a quick way to do this or do it edit code for 45 nodes?).

So far, I managed to make the first change, however, whenever the user enters something, the label is not updated automatically, the user needs to select something else from the tree view and return to the original text selection to update.

Here is my code:

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
        if (treeView1.SelectedNode.FullPath == @"Node0/Node1")
        {
            label1.Text = String.Format("Whatever default text there is {0}"
     textBox1.Text);
        }
     }
}

}

Shown here is a screenshot when it is in default mode.

http://i.stack.imgur.com/0NOlP.jpg

Here is a screenshot when I entered the text, but there are no changes in the label field:

http://i.stack.imgur.com/3uX53.jpg

Thank you in advance.

+3
source share
3 answers

It sounds like you just need to add an event handler TextChangedto your control textbox1. Try putting this in your constructor Form1:

textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

Then add this method:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    label1.Text = String.Format("Whatever default text there is {0}", textBox1.Text)
}
+6
source

, TextChanged :

private void textBox1_TextChanged(object sender, EventArgs e)
{
    label1.Text = String.Format("Whatever default text there is {0}", textBox1.Text); 
}

, .

+1

label1.Text = String.Format ("Your text is here");

-1
source

All Articles