写入文本文件不匹配

时间:2013-06-26 06:47:21

标签: c# split text-files

我有一个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个逗号,我的代码逻辑错了吗?因为我一直在寻找它几个小时,我仍然无法解决它。抱歉,如果我的逻辑不好,我是编程的新手。

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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。