【问题标题】:How to add textbox array value to string array?如何将文本框数组值添加到字符串数组?
【发布时间】:2020-04-05 07:53:11
【问题描述】:

如何将文本框[] 值添加到字符串[]

private void button3_Click(object sender, EventArgs e)
{
    string[] text = new string[DT.Columns.Count];
    string[] textb = new string[panel1.Controls.Count];

    // Below is the programmatically textbox

    foreach (Control C in panel1.Controls)
    {
        if (C is TextBox)
        {
            for (int m = 0; m < DT.Columns.Count; m++)
            {

                // This is the place that I want to add the textbox value to string array

                textb[m] = C.Text[m].ToString();
                MessageBox.Show(textb[m]);
            }
        }
    }

    foreach (DataColumn DC in DT.Columns)
    {
        for (int k = 0; k < DT.Columns.Count; k++)
        {
            text[k] = DC.Table.Columns[k].ToString();
        }
    }

【问题讨论】:

    标签: c# winforms textbox


    【解决方案1】:

    这很容易通过LINQ 实现。您可以使用:

    string[] textb = panel1.Controls
                           .OfType<TextBox>()
                           .Select(t => t.Text)
                           .ToArray();
    

    【讨论】:

    • 解释为什么这个解决方案有效也同样重要。
    • @RoadRunner 我添加了一个指向 LINQ 文档的链接。如果 OP 熟悉 LINQ,代码是不言自明的。
    猜你喜欢
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多