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

StackOverflow https://stackoverflow.com/questions/23353147

Вопрос

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.

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.

Это было полезно?

Решение

You can use 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
}

:)

Другие советы

Your if statements can be replaced by something similar to the following:-

for (int i = 0; i + 30 <= text.Length; i += 30)
{
    listBox7.Items.Add(string.Join(",", text, i, 30));
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top