链接动态创建的文本框和标签
我创建了一个文本框数组和一个标签数组。当文本框中的信息更新时,我希望它更改标签。我怎样才能做到这一点?下面是我的一段代码。我还没有创建我认为需要帮助的部分的 EvenHandler。全部采用 C# 语言,使用 Windows 应用程序形式。
textBoxes = new TextBox[value];
labels = new Label[value];
for (int i = 1; i < value; i++)
{
textBoxes[i] = new TextBox();
textBoxes[i].Location = new Point(30, ToBox.Bottom + (i * 43));
labels[i] = new Label();
labels[i].Location = new Point(TopBox3[i].Width + 140, TopBox3[i].Top +3);
textboxes[i].ValueChanged += new EventHandler(this.TextBox_ValueChanged) ;
this.Controls.Add(labels[i]);
this.Controls.Add(textBoxes[i]);
}
I created an array of of TextBoxes and an array of Labels. When the information is updated in the TextBox I want it to change the Labels. How would I be able to do this? Below is piece of my code. I have not created the EvenHandler that I think is the part I need help with. All in C# using windows application form.
textBoxes = new TextBox[value];
labels = new Label[value];
for (int i = 1; i < value; i++)
{
textBoxes[i] = new TextBox();
textBoxes[i].Location = new Point(30, ToBox.Bottom + (i * 43));
labels[i] = new Label();
labels[i].Location = new Point(TopBox3[i].Width + 140, TopBox3[i].Top +3);
textboxes[i].ValueChanged += new EventHandler(this.TextBox_ValueChanged) ;
this.Controls.Add(labels[i]);
this.Controls.Add(textBoxes[i]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以记住 Tag 属性中 TextBox 的索引
,然后在事件处理程序中使用该值来获取相应的标签(假设您将标签数组保存为局部变量)
You can remember the index of the TextBox in the Tag property
and then use this value in your eventhandler to get the corresponding label (assuming that you hold the labels array as a local variable)
您应该编写如下内容:
在方法中,文本已更改的 TextBox 作为“发送者”传递。您在数组中查找它,因此您确定了索引“i”,该索引可用于访问相应的标签并设置其文本。
顺便说一句,正如 Tim 所说,该事件是 TextChanged,而不是 ValueChanged。此外请注意,文本中的每次更改都会触发该事件,即只要您按下某个键,标签就会更新。如果您希望仅在用户完成输入文本时更新标签,那么您应该使用“离开”事件。
You should write something like this:
In the method the TextBox whose text has changed is passed as "sender". You look into your array for it, so you identify the index "i" which can be used to access the corresponding Label and to set its text.
BTW as Tim said, the event is TextChanged, not ValueChanged. Furthermore be aware that the event is triggered for every change in the text, i.e. as soon as you press a key the label will be updated. If you prefer to update your labels only when the user has finished to enter its text Leave is the event you should use.