0
votes

Here is the add playlist code in C#

//toolstrip button to create new playlist
private void newPlaylistToolStripMenuItem_Click(object sender, EventArgs e)
{
    //CreatePlaylist();

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

    ListBox listbox22 = new ListBox();
    listbox22.Dock = DockStyle.Fill;
    listbox22.SelectionMode = SelectionMode.MultiExtended;

    tab.Controls.Add(listbox22);

}

Here is play button code:

private void Play_Click(object sender, EventArgs e)
{   
    double pausedPosition = WMPPlayer.Ctlcontrols.currentPosition;
    WMPPlayer.URL = filepaths[listBox1.SelectedIndex];
    WMPPlayer.Ctlcontrols.currentPosition = pausedPosition;
    WMPPlayer.Ctlcontrols.play();
}    

ADDING MUSIC TO TABS CODE:

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

        listBox1.SelectionMode = SelectionMode.MultiExtended;
        ListBox current_listbox = (ListBox)tabControl1.SelectedTab.Controls[0];
    }

When we reference listbox1 in the play button code, that is the main listbox in the middle of the screen before you make any new tabs. This is a music player and the new tabs are playlists that users can add music to. When I try to play a song through the new tab, nothing plays. How can I make the play button play songs in these newly formed tabs/listboxes?

1

1 Answers

0
votes

You will have to find the active controls yourself. Since it looks like your TabPages only have one control on them, you can simply access the first control in the Controls collection:

private void Play_Click(object sender, EventArgs e)
{
  ListBox lb = tabControl1.SelectedTab.Controls[0] as ListBox;
  if (lb != null && lb.SelectedIndex > -1) {
    double pausedPosition = WMPPlayer.Ctlcontrols.currentPosition;
    WMPPlayer.URL = filepaths[lb.SelectedIndex];
    WMPPlayer.Ctlcontrols.currentPosition = pausedPosition;
    WMPPlayer.Ctlcontrols.play();
  }
}

If there are multiple controls on your TabPages, then you can loop through your controls and look for a ListBox type or give your controls a specific name that you can use as a key.