从动态创建的 WinForms 文本框中获取文本 [英] Get text from dynamically created WinForms textbox

查看:23
本文介绍了从动态创建的 WinForms 文本框中获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如你在方法中看到的那样,我正在制作一个带有动态创建的文本框的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆