Monday, March 7, 2011

Make a playlist of artists in Visual Basic using an ArrayList

Use a textbox to add Artists to a combobox then clear it out when you get all the Artists added.

 Public Class Form1  
Dim ArtistList As New ArrayList
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'take stuff out of text box, put stuff into the combobox, clear out textbox
ArtistList.Add(txtArtistTitle.Text)
ComboBox1.DataSource = Nothing
ComboBox1.DataSource = ArtistList
txtArtistTitle.Clear()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
ComboBox1.Text = Nothing
ArtistList.Clear()
ComboBox1.DataSource = Nothing
End Sub
End Class




* Add a textbox, combobox, and a button to a form.
* When you type something in the textbox, it will add the text to an arraylist. The arraylist will be defined at the class level, not in the click event.
* When the button is clicked, you also want to set your arraylist to the datasource property of the combobox.
* In order to refresh the combobox each time something is added, you need to set the datasource literally to the word Nothing and then set your datasource again to your arraylist on the next line. This will refresh the combobox contents
* Also add a button that when clicked, it will remove all the contents from the arraylist and refresh the combobox to be empty.

No comments:

Post a Comment