简体   繁体   English

如何将文本框数组值添加到字符串数组?

[英]How to add textbox array value to string array?

How to add textbox[] value to string[]如何将文本框 [] 值添加到字符串 []

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();
        }
    }

This is very easy to achieve with LINQ .这很容易通过LINQ实现。 You can just use:你可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM