Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Okay I am practicing programming (I'm new) and I'm making a simple audio player.

The form is like this:

There is 2 ListBoxes, 1 is for Importing Music to the next ListBox, which is the Music listbox.
You use a button to find a song file in an open file dialog which then puts the file name into the listBox1. From there you can highlight it and send it to listBox2 with another button.

However.... The filename from the open file dialog is like C:\Users\Joel\Music\song.wav so I had used code to shorten all the songs down to just song.wav for example.
But this causes problems. I want the visible song name to be "Song.wav" but still have some sort of background information so that when you click the Play button, it takes information (the file location to play it in SoundPlayer) instead of the name.

How can I give each item entered to the listbox1 some background information which it can carry with it around to listbox2 and etc.
I have been thinking for hours but I can't work it out, Maybe an array? to store all the songs in the list? But I don't have a clue how to work it :s

I really really really need help. I will try rephrase if I was unclear about anything
Thanks!
Posted

1 solution

This can be one possible solution. Create a class called MusicFile which has FileName and Path properties.
C#
public class Song{
   public string Path {get;set;}
   public string SongName {get;set;}
}

Then create the music list by loading the folder which contains the music files assigning the Path and SongName(removed file path)
C#
 // Assuming that you are using Window Form
 protected void Form_Load(object sender, EventArgs e) {
   bindList();
 } 
 private void bindList{
   List<Song> songs = new List<song>();
   // for each files in folder 
      songs.Add(new Song() { Path="C:\\Users\\Joel\\Music\\song.wav", SongName="song" } );
   // Finally
   listBox1.DataSource = songs;
   listBox1.DisplayMember = "SongName";
   listBox1.ValueMember = "Path";
}

private void listBox1_SelectedIndexChanged(Object sender, System.EventArgs e)
{
    // Get the currently selected item in the ListBox.
    string song = listBox1.SelectedValue.ToString();
    // Do play or do something.
} 
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 26-Jan-12 20:53pm    
I fixed some bugs for your, one of then due to formatting: <song> created parasitic tags due to auto-formatting, case became low. Under the edit box there is an item "encode", use it to escape HTML characters by using HTML entities.

The bugs fixed: string literal needed "", not '', also the literal assigned to Path -- added "@" (verbose), otherwise it would not compile due to '\'.

Now, something which I did not fix as it would be too much: Form_Load and listBox1_SelectedIndexChanged are event handlers; and it is not shown where they are added to what invocation lists. For a code sample, you should better not assume that it is done by Designer. Generally, Designer is a bad thing for learning and it's also a bad thing for advanced UI. I usually show anonymous syntax. What you show does not show what events are handled.

Finally, the two methods violate (good) Microsoft naming conventions. Yes, the Designer violates them, so what? Nobody tell us than the auto-generated names are supposed to be used as is. They should be given semantic names instead, using refactoring engine.

So, I don't know... well, I'll vote 4.
--SA
Wonde Tadesse 26-Jan-12 20:56pm    
Thank you but I already fixed it and hopefully we shouldn't expect a runable code. OP has to learn something as well.
Shivender Thakur 14-Jun-13 1:41am    
its nt wrking i wnt to know where shuld i built this class whether in partial class or separate class nd where should i declare binding function whether in partial class aur music class

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900