private void btnAdd_Click(object sender, EventArgs e) { try { Song mySong = new Song(); mySong = GetSongInfo(mySong);//gets the song attributes string myNewSong = mySong.Title + " by " + mySong.Artist + " (" + mySong.Length + ")";//creates a new string with the attributes. int songCheck = 0; //used below for error checking. foreach (string str in myOrderList)//runs through the myOrderList to see if this song is already in it. { //if it's already in the list, it will increment songCheck and display if (myNewSong == str) //a message box. { songCheck++; } } if (songCheck < 1) { myOrderList.Add(myNewSong); songCount++; } else//if it's not found in the list, the string will be added. { MessageBox.Show("That song is already on your purchase list.", "ERROR"); } txtSongCount.Text = songCount.ToString(); } catch { MessageBox.Show("There was an error trying to add your song. Make sure you have a song selected.", "ADD ERROR"); } }
List<string> songPath = new List<string>(); //list of the paths to preview the songs from. #endregion Fields #region Constructors public frmStore() { InitializeComponent(); StreamReader inputFile;//input for the text file. string line;//stores each line of the text file. string[] lineArray;//used to split each line into a 5 element array for setting object attributes. Song mySong = new Song(); inputFile = File.OpenText("Songs.txt");//the text file with all the info for each song. while (!inputFile.EndOfStream) { line = inputFile.ReadLine(); lineArray = line.Split(',');//splits the line into 5 elements. mySong.Title = lineArray[0];//title=element 1 mySong.Artist = lineArray[1];//artist=element 2 mySong.Length = lineArray[2];//length=element 3 mySong.Genre = lineArray[3];//genre=element 4 mySong.Path = lineArray[4];//path=element 5 mySongList.Add(mySong);//all of these attributes make one song object. //adds each element of the array to a listbox for display to the user. lstArtist.Items.Add(lineArray[1]); lstTitle.Items.Add(lineArray[0]); lstGenre.Items.Add(lineArray[3]); lstLength.Items.Add(lineArray[2]); songPath.Add(lineArray[4]);//adds a new element in the songPath list containing the path to the song. } txtSongCount.Text = songCount.ToString();//sets the order's number of songs to 0 when the form is initialized. inputFile.Close(); }
//used for setting attributes for each song. private Song GetSongInfo(Song mySong) { //sets song attributes. mySong.Title = lstTitle.SelectedItem.ToString(); mySong.Artist = lstArtist.SelectedItem.ToString(); mySong.Genre = lstGenre.SelectedItem.ToString(); mySong.Length = lstLength.SelectedItem.ToString(); return mySong; }