1
votes

I am working on making a music player in c#. I am making music playlists right now and am stuck. As of right now I am using tabControl and a button that adds a tab with an empty listbox in it. Here is the code for that button:

private void button10_Click(object sender, EventArgs e)
{
    TabPage tp = new TabPage("Playlist");
    tabControl1.TabPages.Add(tp);

    ListBox lb = new ListBox();
    lb.Dock = DockStyle.Fill;

    tp.Controls.Add(lb);
}

The problem I am running into is that I do not know how to allow the user to add music to these dynamically created listboxes within the tabs. The main list of music is located in a listbox in the first tab and I want the user to be able to select this music and put it in the new listboxes or "playlists" so I need to reference them somehow.

1
Don't do that. Learn XAML.15ee8f99-57ff-4f92-890c-b56153
Why don't you let the user select music and add those listItems to the new tab?MetaColon
@MetaColon I do not know how to reference the new tab/listbox since the tabs are being dynamically created with the listboxes.csharpsendhelp
Which technology is this? Web Forms? ASP.NET MVC? Win Forms? WPF? Silverlight? Anything else?Uwe Keim
@UweKeim It is Win Forms.csharpsendhelp

1 Answers

0
votes

I'll just assume that you have a button (addToPlayListButton), a textBox (playListName) to add the selected song to the entered playList (tab-) name and that your songs listBox is called songList. I'll furthermore assume that every new playlist has a new tab. In that case you'll have to identify them so I'd change the name of the tabs:

TabPage tp = new TabPage($"Playlist {tabControl1.TabPages.Count}");

So you'll have to handle the button click event from addToPlayListButton like that:

private void onAddToPlayListButton_Click (object sender, EventArgs e) =>
    (tabControl1.TabPages.Cast<TabPage>()
        .FirstOrDefault(page => page.Text == playListName.Text)
        ?.Controls.Cast<Control>()
        .FirstOrDefault(control => control is ListBox) as ListBox)?.Items.Add(songList.SelectedItem);