//Creating album object private void btnAdd_Click(object sender, EventArgs e) { Album toAdd = new Album(); toAdd.Artist = txtArtist.Text; toAdd.Title = txtAlbum.Text; toAdd.Genre = txtGenre.Text; toAdd.ImagePath = addAlbumArt(); //Adding new album to list of albums Albums.Add(toAdd); UpdateList(); }
//Creating album object private void btnAdd_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); Album toAdd = new Album(); toAdd.Artist = txtArtist.Text; toAdd.Title = txtAlbum.Text; toAdd.Genre = txtGenre.Text; openFile.ShowDialog(); toAdd.ImagePath = openFile.FileName; //Adding new album to list of albums Albums.Add(toAdd); UpdateList(); }
private void Form1_Load(object sender, EventArgs e) { StreamReader sr = new StreamReader(Properties.Resources.MusicStorage); String line; while ((line = sr.ReadLine()) != null) { string[] album = line.Split('|'); Album toAdd = new Album(); toAdd.Artist = album[0]; toAdd.Title = album[1]; toAdd.Genre = album[2]; toAdd.DateAdded = DateTime.Parse(album[3]); toAdd.ImagePath = album[4]; } sr.Close(); }
/// <summary> /// this method attempts to load albums from a txt file /// the source parameter is used to define the source file /// in our streamreader object /// </summary> /// <param name="source"></param> private void loadAlbums(string source) { //check if this method was called from the exception in the //form load event if (source == "~~~~~") { //if it is, prompt the user for a new file //to try and read and set that to our source variable OpenFileDialog of = new OpenFileDialog(); of.ShowDialog(); source = of.SafeFileName; } //Create a streamreader opbject and set the source to //the txt file we have stored our albums in StreamReader sr = new StreamReader(source); //this string wil hold our line as a string string line; //this will be the dictionary key //loop while we are not at the end of //our streamreder while (!sr.EndOfStream) { //set the line to our current line line = sr.ReadLine(); //create an array of the elements on our string //and split the string on the | char string[] album = line.Split('|'); //initalize an album object to add Album toAdd = new Album(); //set the elements of our string array //to the properties of our album toAdd.Artist = album[0]; toAdd.Title = album[1]; toAdd.Genre = album[2]; toAdd.DateAdded = DateTime.Parse(album[3]); toAdd.ImagePath = album[4]; //add this album to our dictionary Albums.Add(toAdd); //add one to our key } //close the streamreader so we can overwrite //this file with any changes on exit sr.Close(); }