-3

I have create a empty list which will get used when the user enters new tracks and my interface has listbox and a texbox and add and remove button.

My aim is to when i add a new item into the listbox same button use the function to add that item to a list rather them just adding to a listbox and not storing it.

trackListbox.Items.Add(newTracktextBox.Text);
List<Songs> NewSongs = newTracktextBox.Text ().ToList(); ; this is not correct

Any different ideas?

class Songs
{
    private string trackName;
    private int trackLength;
    public Songs (string trackName, int trackLength)
    {
        this.trackName = trackName;
        this.trackLength = trackLength;
    }
}
Mahdi
  • 3,199
  • 2
  • 25
  • 35
Mobster007
  • 3
  • 1
  • 4

4 Answers4

3

Try this

Songs objSong = new Songs(newTracktextBox.Text,0); // define your length instead of 0

List<Songs> NewSongs = new List<Songs>();
NewSongs.Add(objSong);
Ankit
  • 760
  • 6
  • 15
0

Your newTracktextBox variable is not an object of type Song.

You should create a new object of type Song with the text that's in newTracktextBox and add the new object to the list

rubenj
  • 118
  • 1
  • 6
0
public class Songs{
    String TrackName;
    int TrackLength;
    public Songs(string trackName, int trackLength){
       this.TrackName = trackName;
       this.TrackLength = trackLength;
   }
   //methods
}

make a list of songs

List<Songs> NewSongs = new List<Songs>();

add the new song to the list by

int tracklength = 50; // set the tracklength where you need
NewSongs.Add(new Songs(TextBox.Text.ToString(),tracklegnth));

Note that the ToString() method maybe is redudant.

hope i helped

Setar
  • 140
  • 10
  • I think your on to the something good. ill get back to you. Thanks – Mobster007 Dec 01 '16 at 13:11
  • I have created second textbox which replaces your tracklength but only problem i have that i need to convert text from textbox to int – Mobster007 Dec 01 '16 at 13:21
  • From now you have to search a little but before posting a question what you need is here http://stackoverflow.com/questions/2709253/converting-a-string-to-an-integer-on-android – Setar Dec 01 '16 at 13:25
0

It's good practice to name the class Song instead of Songs since it will represent only one song.


With adding songs manually to the listBox

private List<Song> SongList;

public Form1()
{
    InitializeComponent();

    SongList = new List<Song>();
}

private void button1_Click(object sender, EventArgs e)    
{
    Song song = new Song(newTracktextBox.Text, 100);
    SongList.Add(song);
    listBox1.Items.Add(song); // The trackName will be shown because we are doing a override on the ToString() in the Song class
}

class Song
{
    private string trackName;
    private int trackLength;

    public Song(string trackName, int trackLength)
    {
        this.trackName = trackName;
        this.trackLength = trackLength;
    }

    public override string ToString()
    {
        return trackName;
        // Case you want to show more...
        // return trackName + ": " +  trackLength;
    }
}

With automatic binding by using a BindingList<Song>

private BindingList<Song> SongList;

public Form1()
{
    InitializeComponent();

    // Initialise a new list and bind it to the listbox
    SongList = new BindingList<Song>();
    listBox1.DataSource = SongList;
}


private void button1_Click(object sender, EventArgs e)
{
    // Create a new song and add it to the list, 
    // the listbox will automatically update accordingly
    Song song = new Song(newTracktextBox.Text, 100);
    SongList.Add(song);
}

class Song
{
    private string trackName;
    private int trackLength;

    public Song(string trackName, int trackLength)
    {
        this.trackName = trackName;
        this.trackLength = trackLength;
    }

    public override string ToString()
    {
        return trackName;
    }
}

Result

result

Jim
  • 2,974
  • 2
  • 19
  • 29