0

So this is a part of a university assignment that I am stuck on, I am to create a media player and the code I am currently struggling with should read from a .txt file into an array, and then it should read the track title into a listbox named Lst_genre, my code so far is as follows (I am quite new to the website so let me know if I need to put anything else.)

private void Form1_Load(object sender, EventArgs e)
{
      string[] readFromfile = File.ReadLines("filepathgoeshere").ToArray();
      ListBox listBox1 = new ListBox();
      // add items 
      Lst_Genre.Items.Add(readFromfile);
      // add to controls 
      Controls.Add(listBox1);
}

the format of the text file is as follows

2
hip hop // i am wanting this to the textbox
eminem- without me.mp3 // both mp3 files should show in Lst_genre
eminem- lose yourself.mp3

The genre name should read into a textbox above also but at the moment I am more concerned with the track names. Would be great if anybody could give some input as I am currently at a loss.

5
  • What exactly are you stuck on? What is the value of readFromFile? Commented Apr 16, 2018 at 12:42
  • Please read How to Ask. What are you struggling with and stuck on, exactly? Do you need one list of genres which, when selected, populates the song list? Commented Apr 16, 2018 at 12:42
  • At the moment when I run my code, the listbox just displays "string[]array" when I want it to display the text that is meant to be read from the file and a random listbox shows up in the top left corner whereas I want to create this to hold my tracks from other genres on a horizontal scrollbar Commented Apr 16, 2018 at 12:46
  • Where do you define Lst_Genre? Commented Apr 16, 2018 at 12:57
  • Lst_Genre is the name of the listbox that I set up on my form that I am trying to print the data from the file too, sorry for any miscommunication I am still quite new to programming and it is something that despite enjoying, I struggle with a bit. Commented Apr 16, 2018 at 13:00

1 Answer 1

3

You have to extract data first:

var readFromFile = File
  .ReadLines("filepathgoeshere")
  .Skip(1)                                                  // Skip title 
  .Select(line => line.Substring(0, line.LastIndexOf(' '))) // get genre (not number) 
  .ToArray();                                               // we want an array

Then add all the items: AddRange:

Lst_Genre.Items.AddRange(readFromfile);

Edit 1: As far as I can see from the comments

I have a textbox above my listbox to hold the genre name and the listbox should hold my track names

We actually have to provide data for two controls:

var allLines = File
  .ReadAllLines("filepathgoeshere");

then

// Top Line: genre name
myTextBox.Text = allLines[0]; 

// tracks
Lst_Genre.Items.AddRange(allLines
  .Skip(1)   // Here we skip top line (genre name?)
  .Select(line => line.Substring(0, line.LastIndexOf(' ')))
  .ToArray());

Edit 2: According to the example provided:

var allLines = File
  .ReadAllLines("filepathgoeshere");

// Genre name is the second line (top one is id which we skip)
myTextBox.Text = allLines[1]; 

// in case you want to clear existing control, not creating a new one. 
Lst_Genre.Items.Clear();

// Tracks
Lst_Genre.Items.AddRange(allLines
  .Skip(2)     // Here we skip tow lines (id and genre name)
  .ToArray());
Sign up to request clarification or add additional context in comments.

15 Comments

Thankyou very much, the information is being read from the file, however on this line code(line => line.Substring(0, line.LastIndexOf("Other"))) I am being hit with length cannot be less than 0
@George taylor: Sorry? I'm trying to get rid of the id part which I expect to be after the last space, e.g. "track #12 123" "track #12". If I've got the format wrong, please *provide examples
So the part that I called id is the genre number, I have this as I will have to do the same thing with a few files and this is how the txt files have been given to us, I have a textbox above my listbox to hold the genre name and the listbox should hold my track names, using a horizontal scrollbar when in run time my program is meant to move onto another listbox where the other files will be read in. I apologise for the lengthy description :p
@George taylor: Could you please, provide the initial line (example), and the desired outcome? E.g. "track #12 123" -> "track #12"?
so at runtime it should look like this (genre name here(genreTextbox)) below that is my listbox named Lst_Genre and I need to display track1 -> track 2 here
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.