Question I Need Help With CheckedListBox Values

Saad

New member
Joined
Feb 2, 2011
Messages
1
Programming Experience
Beginner
Hey

I'm doing a class project, and I keep running into issues with the checkedlistbox.

So here's the scenerio:



I have a form with one CheckedListBox, one button, and one textbox.



I want that when you press the button that the items selected inside the CheckedListBox should be displayed in the textbox.

Note: I want multiple selections.



Now, I know that if I type in the following code it will only show me one of the selections inside the checkedlistbox with the lowest index:



Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

TextBox1.Text = CheckedListBox1.SelectedItem.ToString()

End Sub


But how will I show multiple values inside textbox1?

I've done a little research and have discovered that changing the line:



TextBox1.Text = CheckedListBox1.SelectedItem.ToString()


to something like:



TextBox1.Text = CheckedListBox1.SelectedItems.ToString()


will supposedly display all the multiple selections inside the checkedlistbox . . . but when I debug the program it only shows the following inside the textbox:

System.Windows.Forms.ListBox+SelectedObjectCollection
 
SelectedItems is a collection, so you can't just call ToString on it. You have to get the multiple values individually and then add them to a list or the append them to a string or the like.

There are various options. The traditional approach would be to create a String array of the appropriate size and then call CopyTo on the SelectedItems. Once you've populated the array, you can assign it to the Lines property of the TextBox or call String.Join to create a single String.
 

Latest posts

Back
Top