I am trying to display specific lines that i have now stored in an array from a text file into a list box.

string[] lines = File.ReadAllLines(@"C:\Users\James Dunn\Documents\Visual Studio 2012\Projects\Assignment 2\Assignment 2\MyJukeBox\bin\Debug\Media\Genre.txt");
mediaLibrary[0] = genreListBox;
for (int l = 3; l < lines.Length; l++)
{
   mediaLibrary[0].Items.Add(lines[l]);
}
genreListBox.Items.Add(mediaLibrary[0]);

This code so far displays all but the first 3 lines of text. how do i get it to stop after its displayed the lines I require? I have tried using the following in the for loop but it gives me errors.

int ll = 6; != lines.Length;

My txt file layed out like so

3
2
Other
Big Bad John (1961) - Jimmy Dean.mp3
My Ding A Ling - Chuck Berry.mp3
2
Rap
2pac-ghost.mp3
B.I.G-Spit Your Game.mp3
1
Rock
Linkin park-Numb.mp3
有帮助吗?

解决方案 2

I managed to solve my problem by using an if statement and adding break.

{
// Adding first genre title
string[] lines = File.ReadAllLines(@"C:\Users\James Dunn\Documents\Visual Studio 2012\Projects\Assignment 2\Assignment 2\MyJukeBox\bin\Debug\Media\Genre.txt");
for (int l = 3; l < lines.Length; l++)
{
mediaLibrary[0].Items.Add(lines[l]);
if (l == 4)
break;
}
genreTitleTextBox.(mediaLibrary[0]);
}

其他提示

You could use Skip() and Take() if the elements are always in the same position.

So if you always wanted the 3rd and 4th lines you would do

mediaLibrary[0].Items.AddRange(lines.Skip(2).Take(2).ToArray<object>());
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top