简体   繁体   English

划分文本文件并将其显示到列表框中,列表框中的每一行将有30行文本文件,用逗号分隔

[英]Dividing a textfile and show it into listbox, where each line in the listbox will have 30 lines of textfile seperated by comma

I am trying to divide a text file and show it into listbox, where each line in the list will have 30 lines of the textfile seperated by comma. 我试图划分一个文本文件并将其显示到列表框中,其中列表中的每一行将有30行由逗号分隔的文本文件。

I did this in a very bad process, I know there is good solution for this, please help me to sort out. 我在一个非常糟糕的过程中做到了这一点,我知道这有很好的解决方案,请帮我整理一下。

This is my code : 这是我的代码:

Taking users input and storing it in listbox 用户输入并将其存储在列表框中

OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Text Files|*.txt";
            openFileDialog1.Title = "Select a Text file";
            openFileDialog1.FileName = "";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string file = openFileDialog1.FileName;

                string[] text = System.IO.File.ReadAllLines(file);



                if (text.Length == 30)
                {
                    listBox7.Items.Add(string.Join(",", text[0], text[1], text[2], text[3], text[4], text[5], text[6], text[7], text[8], text[9], text[10], text[11], text[12], text[13], text[14], text[15], text[16], text[17], text[18], text[19], text[20], text[21], text[22], text[23], text[24], text[25], text[26], text[27], text[28], text[29]));
                }
                if (text.Length == 60)
                {
                    listBox7.Items.Add(string.Join(",", text[0], text[1], text[2], text[3], text[4], text[5], text[6], text[7], text[8], text[9], text[10], text[11], text[12], text[13], text[14], text[15], text[16], text[17], text[18], text[19], text[20], text[21], text[22], text[23], text[24], text[25], text[26], text[27], text[28], text[29]));
                    listBox7.Items.Add(string.Join(",", text[30], text[31], text[32], text[33], text[34], text[35], text[36], text[37], text[38], text[39], text[40], text[41], text[42], text[43], text[44], text[45], text[46], text[47], text[48], text[49], text[50], text[51], text[52], text[53], text[54], text[55], text[56], text[57], text[58], text[59]));
                }
                if (text.Length == 90)
                {
                    listBox7.Items.Add(string.Join(",", text[0], text[1], text[2], text[3], text[4], text[5], text[6], text[7], text[8], text[9], text[10], text[11], text[12], text[13], text[14], text[15], text[16], text[17], text[18], text[19], text[20], text[21], text[22], text[23], text[24], text[25], text[26], text[27], text[28], text[29]));
                    listBox7.Items.Add(string.Join(",", text[30], text[31], text[32], text[33], text[34], text[35], text[36], text[37], text[38], text[39], text[40], text[41], text[42], text[43], text[44], text[45], text[46], text[47], text[48], text[49], text[50], text[51], text[52], text[53], text[54], text[55], text[56], text[57], text[58], text[59]));
                    listBox7.Items.Add(string.Join(",", text[60], text[61], text[62], text[63], text[64], text[65], text[66], text[67], text[68], text[69], text[70], text[71], text[72], text[73], text[74], text[75], text[76], text[77], text[78], text[79], text[80], text[81], text[82], text[83], text[84], text[85], text[86], text[87], text[88], text[89]));
                }
}

//and so on

Please help. 请帮忙。

Your if statements can be replaced by something similar to the following:- 您的if语句可以替换为类似于以下内容: -

for (int i = 0; i + 30 <= text.Length; i += 30)
{
    listBox7.Items.Add(string.Join(",", text, i, 30));
}

You can use linq: 你可以使用linq:

string[] text = System.IO.File.ReadAllLines(file);

var iteration = 0;
var bunch_size = 30;
var times_to_loop = text.Length / bunch_size;

while (iteration <= times_to_loop ) {
    listBox7.Items.Add(
           string.Join(",", 
               // Next 2 lines will add the next bunch size (30 in your case)
               text.Skip(iteration*bunch_size)
                   .Take(bunch_size))            
           );
    iteration++; // so loop ends
}

:) :)

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

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