收集设计中创建的文本框

发布于 2025-01-06 13:25:17 字数 86 浏览 2 评论 0原文

我通过拖放添加了几个文本框。现在我想将它们全部收集在一个文本框数组下。我知道如何在代码中创建文本框数组,但不知道如何收集设计期间创建的文本框。有人可以帮忙吗?

I added several textboxes by drag_n_dropping. Now I want to gather them all under a textbox array. I know how to create array of textboxes in code but not how to gather the textboxes created during design. Could anyone help please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

遮云壑 2025-01-13 13:25:17

有时,文本框不会直接放置在表单上,​​而是放置在容器控件(例如选项卡控件或拆分容器)上。如果您想找到所有这些文本框,递归会有所帮助

private List<TextBox> _textboxes = new List<TextBox>();

private void GetTextBoxes(Control parent)
{
    foreach (Control c in parent.Controls) {
        var tb = c as TextBox;
        if (tb != null) {
            _textboxes.Add(tb);
        } else {
            GetTextBoxes(c);
        }
    }
}

然后您通过将表单作为参数传递来调用 GetTextBoxes

GetTextBoxes(this);

这是可能的,因为 Form 本身派生自 Control

Sometimes the textboxes are not placed on the form directly but on a container control like a tab control or a split container. If you want to find all these textboxes, a recursion will help

private List<TextBox> _textboxes = new List<TextBox>();

private void GetTextBoxes(Control parent)
{
    foreach (Control c in parent.Controls) {
        var tb = c as TextBox;
        if (tb != null) {
            _textboxes.Add(tb);
        } else {
            GetTextBoxes(c);
        }
    }
}

Then you call GetTextBoxes by passing the form as argument

GetTextBoxes(this);

This is possible, since Form itself derives from Control.

毁虫ゝ 2025-01-13 13:25:17

这假设您的文本框位于同一个 GroupBoxPanel 中。

var groupOfTextBoxes = groupBox1.Controls.OfType<TextBox>();
MessageBox.Show(groupOfTextBoxes.Count().ToString());

var textBoxesWithinForm = this.Controls.OfType<TextBox>();
MessageBox.Show(textBoxesWithinForm.Count().ToString());

需要使用 System.Linq;。请注意,textBoxesWithinForm 将忽略 groupBox 内的 TextBox,反之亦然。

或者像 @Jeff 建议的那样,但不要通过 this.Controls 并比较 Control 是否是 Textbox:

 foreach (TextBox in this.Controls.OfType<TextBox>())
 {
     //add to your array
 }

This assumes your TextBoxes are within same GroupBox or Panel.

var groupOfTextBoxes = groupBox1.Controls.OfType<TextBox>();
MessageBox.Show(groupOfTextBoxes.Count().ToString());

var textBoxesWithinForm = this.Controls.OfType<TextBox>();
MessageBox.Show(textBoxesWithinForm.Count().ToString());

Requires using System.Linq;. Please note that textBoxesWithinForm will ignore TextBoxes that are within groupBox and vice versa.

Or like @Jeff suggests but instead of going thru this.Controls and comparing if Control is Textbox:

 foreach (TextBox in this.Controls.OfType<TextBox>())
 {
     //add to your array
 }
何以笙箫默 2025-01-13 13:25:17
foreach (Control c in this.Controls)
  {
    if (c is TextBox)
    {
       //add to your array
    }
  }
foreach (Control c in this.Controls)
  {
    if (c is TextBox)
    {
       //add to your array
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文