0

我有一个 10x10 的文本框(其中 100 个)我编写此代码以写入文本文件:

foreach (Control control in Panel1.Controls)
             {


                 var textBox = control as TextBox;
                 if (textBox != null)
                 {
                     if (string.IsNullOrEmpty(textBox.Text)) // ignore this
                     {

                         textBox.Style["visibility"] = "hidden";
                     }
                     textBox.Enabled = false;

                     if (numberofCommas > 8)
                     {
                         stringWriter.Write(textBox.Text);
                         numberofCommas = 0;
                     }
                     else
                     {
                        stringWriter.Write("," + textBox.Text );
                        numberofCommas++;
                         recordsWritten++;
                     }

                     if (recordsWritten == 10)
                     {
                         stringWriter.WriteLine();
                         recordsWritten = 0;
                     }
                     else
                     {

                     }

从上面我想在文本文件中有 10 行 9 个逗号,但是我在文本文件中有 9 行 10 个逗号,我的代码逻辑错了吗?因为我已经找了好几个小时了,我仍然无法解决它。抱歉,如果我的逻辑不好,我是编程新手。

4

2 回答 2

1

我不建议您使用 100 个 TextBox 对象,您可以使用绑定到具有 10 行和 10 列的 DataTable 的 DataGridView。您仍然可以编辑数据并将其保存到文件中。

试试下面的代码

    StringWriter stringWriter1 = new StringWriter();
    DataTable dataTable1 = new DataTable();

    private void Form1_Shown(object sender, EventArgs e)
    {
        dataGridView1.AllowUserToAddRows = false;

        int i;

        for (i = 0; i < 10; i++)
        {
            dataTable1.Columns.Add("Column" + (i + 1), typeof(string));
        }

        for (i = 0; i < 10; i++)
        {
            DataRow dataRow1 = dataTable1.NewRow();
            dataTable1.Rows.Add(dataRow1);
        }

        dataGridView1.DataSource = dataTable1;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string rowString = "";

        int i,j;

        for (i = 0; i < 10; i++)
        {
            rowString = "";

            for (j = 0; j < 10; j++)
            {
                if (dataTable1.Rows[i][j].ToString().Contains(",") == true)
                {
                    //Enclosing the field data inside quotes so that it can
                    //be identified as a single entity.
                    rowString += "\"" + dataTable1.Rows[i][j] + "\"" + ",";
                }
                else
                {
                    rowString += dataTable1.Rows[i][j] + ",";
                }
            }

            rowString = rowString.Substring(0, rowString.Length - 1);
            stringWriter1.WriteLine(rowString);
        }
    }

您只需将 DataGridView 添加到您的表单中。

于 2013-06-26T09:03:27.083 回答
1

我认为你应该recordsWritten在最后一步增加:

if (numberofCommas > 8)
{
     stringWriter.Write(textBox.Text);
     numberofCommas = 0;
     recordsWritten++;
}

这是使用 Linq 的更好方法:

var textBoxes = Panel1.Controls.OfType<TextBox>().Select((t, i) => new { TextBox = t, Index = i }).ToList();
foreach (var tb in textBoxes)
{
    if (string.IsNullOrEmpty(tb.TextBox.Text))
        tb.TextBox.Style["visibility"] = "hidden";
    tb.TextBox.Enabled = false;
}

foreach (var line in textBoxes.GroupBy(e => e.Index / 10)
                        .Select(e => 
                            string.Join(", ", 
                                e.Select(a => a.TextBox.Text).ToArray())))
    stringWriter.WriteLine(line);
于 2013-06-26T07:47:14.717 回答