链接动态创建的文本框和标签

发布于 2024-12-02 17:43:43 字数 616 浏览 0 评论 0原文

我创建了一个文本框数组和一个标签数组。当文本框中的信息更新时,我希望它更改标签。我怎样才能做到这一点?下面是我的一段代码。我还没有创建我认为需要帮助的部分的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听你说爱我 2024-12-09 17:43:43

您可以记住 Tag 属性中 TextBox 的索引

 textBoxes[i].Tag = i;

,然后在事件处理程序中使用该值来获取相应的标签(假设您将标签数组保存为局部变量)

protected void TextBox_ValueChanged(object sender, EventArgs e)
{
  TextBox textbox = sender as TextBox;
  if(textbox==null)
    return;
  int index = Convert.ToInt32(textbox.Tag);
  if(index >= 0 && index < this.labels.Length)
  {
    Label label = this.labels[index];
    /* ... */
  }
}

You can remember the index of the TextBox in the Tag property

 textBoxes[i].Tag = i;

and then use this value in your eventhandler to get the corresponding label (assuming that you hold the labels array as a local variable)

protected void TextBox_ValueChanged(object sender, EventArgs e)
{
  TextBox textbox = sender as TextBox;
  if(textbox==null)
    return;
  int index = Convert.ToInt32(textbox.Tag);
  if(index >= 0 && index < this.labels.Length)
  {
    Label label = this.labels[index];
    /* ... */
  }
}
妥活 2024-12-09 17:43:43

您应该编写如下内容:

    private void textBox1_ValueChanged(object sender, EventArgs e)
    {
        TextBox changedTxt = sender as TextBox;
        for (int i = 1; i < value; i++)
            if (textBoxes[i] == changedTxt)
            {
                Label lblToChange = labeld[i];
                lblToChange.Text = changedTxt.Text;
                break;
            }
    }

在方法中,文本已更改的 TextBox 作为“发送者”传递。您在数组中查找它,因此您确定了索引“i”,该索引可用于访问相应的标签并设置其文本。

顺便说一句,正如 Tim 所说,该事件是 TextChanged,而不是 ValueChanged。此外请注意,文本中的每次更改都会触发该事件,即只要您按下某个键,标签就会更新。如果您希望仅在用户完成输入文本时更新标签,那么您应该使用“离开”事件。

You should write something like this:

    private void textBox1_ValueChanged(object sender, EventArgs e)
    {
        TextBox changedTxt = sender as TextBox;
        for (int i = 1; i < value; i++)
            if (textBoxes[i] == changedTxt)
            {
                Label lblToChange = labeld[i];
                lblToChange.Text = changedTxt.Text;
                break;
            }
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文