从动态创建的 WinForms 文本框中获取文本

编程入门 行业动态 更新时间:2024-10-14 20:21:14
本文介绍了从动态创建的 WinForms 文本框中获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

正如你在方法中看到的那样,我正在制作一个带有动态创建的文本框的 windowsform.

I'm making a windowsform with dynamically created textboxes as u see in the method.

    public void createPassengerBoxes(int numPassenger)
    {
        TextBox[] passengerBoxes = new TextBox[numPassenger];

        for (int u = 0; u < passengerBoxes.Count(); u++)
        {
            passengerBoxes[u] = new TextBox();
        }
        int i = 0;
        foreach (TextBox txt in passengerBoxes)
        {
            string name = "passenger" + i.ToString();

            txt.Name = name;
            txt.Text = name;
            txt.Location = new Point(244, 32 + (i * 28));
            txt.Visible = true;
            this.Controls.Add(txt);
            i++;
        }
    }
}

如何访问框中的文本?

推荐答案

虽然我不确定在什么时候,或者基于什么操作,你想要获取数据,但这里有一个非常通用的方法:

While I'm not sure at what point, or based on what action, you'd like to fetch the data, here's very generic method:

private String[] GetTextBoxStrings()
{
    List<String> list = new List<String>();
    foreach (Control c in this.Controls)
    {
        if (c is TextBox)
            list.Add(((TextBox)c).Text);
    }
    return list.ToArray();
}

这篇关于从动态创建的 WinForms 文本框中获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 20:35:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1398902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   框中   动态   WinForms

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!