C# 如何在文本框更改时动态更新标签文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10147638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to dynamically update Label text when textbox changes
提问by Vallar
I am having a problem with updating the text of my label. Not sure how do I go about doing this.
我在更新标签文本时遇到问题。不知道我该怎么做。
I have a label (lable1) and a text box (secondTextBox)and I have a tree view that the user needs to select items from. The process goes like this:
我有一个标签 (lable1) 和一个文本框 (secondTextBox),我有一个树视图,用户需要从中选择项目。这个过程是这样的:
User selects an element in the tree view, label1 displays default text and secondTextBox appears. When the user changes the default text inside secondTextBox the text inside label1 should automatically update itself without the user pressing anything (bear in mind that I have about 45 nodes that needs this to be active, is there a quick way to do this or do I have to edit the code for the 45 nodes?).
用户在树视图中选择一个元素,label1 显示默认文本并出现 secondTextBox。当用户更改 secondTextBox 内的默认文本时,label1 内的文本应自动更新,而无需用户按任何操作(请记住,我有大约 45 个节点需要将其激活,是否有快速的方法来执行此操作或我必须编辑 45 个节点的代码?)。
So far I was able to do the first change, however whenever the user enters anything, the label doesn't update automatically, the user has to select something else from the tree view and goes back to the original selection for the text to update.
到目前为止,我能够进行第一次更改,但是每当用户输入任何内容时,标签不会自动更新,用户必须从树视图中选择其他内容并返回原始选择以更新文本。
Here is my code so far:
到目前为止,这是我的代码:
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);
}
}
}
}
}
Here is the screen shot for when it is in default mode.
这是处于默认模式时的屏幕截图。
http://i.stack.imgur.com/0NOlP.jpg
http://i.stack.imgur.com/0NOlP.jpg
Here is the screen shot for when I have entered text, but there is no change in the label box:
这是我输入文本时的屏幕截图,但标签框中没有变化:
http://i.stack.imgur.com/3uX53.jpg
http://i.stack.imgur.com/3uX53.jpg
Thank you very much in advance.
非常感谢您提前。
采纳答案by dtown123
It looks like you just need to add a TextChangedevent handler to your textbox1control. Try putting this in your Form1constructor:
看起来您只需TextChanged要向textbox1控件添加一个事件处理程序。尝试将其放入您的Form1构造函数中:
textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
Next, add this method:
接下来,添加此方法:
private void textBox1_TextChanged(object sender, EventArgs e)
{
label1.Text = String.Format("Whatever default text there is {0}", textBox1.Text)
}
回答by Steve
If you want to update your label when the textbox change you should wire the TextChanged events of the textbox:
如果你想在文本框改变时更新你的标签,你应该连接文本框的 TextChanged 事件:
private void textBox1_TextChanged(object sender, EventArgs e)
{
label1.Text = String.Format("Whatever default text there is {0}", textBox1.Text);
}
Set the event using the Form Designer or dinamically when you load your form.
使用表单设计器或在加载表单时动态设置事件。
回答by Hai
label1.Text = String.Format("Your text here");
label1.Text = String.Format("你的文字在这里");

