c# - How to reference/link to a listbox that is dynamically created using a button and tabControl? -
i working on making music player in c#. making music playlists right , stuck. of right using tabcontrol , button adds tab empty listbox in it. here code 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 running not know how allow user add music these dynamically created listboxes within tabs. main list of music located in listbox in first tab , want user able select music , put in new listboxes or "playlists" need reference them somehow.
i'll assume have button (addtoplaylistbutton), textbox (playlistname) add selected song entered playlist (tab-) name , songs listbox called songlist. i'll furthermore assume every new playlist has new tab. in case you'll have identify them i'd change name of tabs:
tabpage tp = new tabpage($"playlist {tabcontrol1.tabpages.count}");
so you'll have handle button click event addtoplaylistbutton 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 listbox) listbox)?.items.add(songlist.selecteditem);
Comments
Post a Comment