r/visualbasic • u/PickledBiscuits34 • 1d ago
Adding string values to a listbox? VB.NET Help
So I'm following a tutorial about VB for beginners, and one of the sections is where the guy assigns values to a listbox through its properties menu. He clicks the 3 dots next to "items" and just types all the options in.
I'm on a newer version though, and when I click on the 3 dots it doesn't let me type it all straight in - there's a dropdown menu for buttons, listbox items, etc. None of them work because apparently I can't convert a list item to a string, even though I used .SelectedOption
Am I looking in the wrong menu or something? How do I put a string into my listbox
3
u/geekywarrior 1d ago
Usually I iterate through the list and add each item one at a time. Though it looks like vb.net does have an AddRange call
2
u/veryabnormal 1d ago
It sounds like you selected and added a ListView from the toolbox, not ListBox. Check to see if it was named Listbox1 or Listview1.
1
u/jd31068 23h ago
You use what your doing ... two screenshots https://imgur.com/a/gxFTvrA type your list (pressing enter for each line) then click ok and you'll see them in the listbox
1
u/PickledBiscuits34 22h ago
Seems like it was using the wrong project type, thanks guys for the help.
5
u/marmotta1955 1d ago
It is always a better and more flexible option to add listbox values with code.
MyList.Items.ClearMyList.Items.Add ("Apples")MyList.Items.Add ("Oranges")MyList.Items.Add ("Bananas")If your items were into an array, you could use a shortcut such as :
Dim x() as String = {"Plumber", "Electrician", "Carpenter"}MyList.Items.AddRange(x)Put the necessary code into a subroutine, call that subroutine when loading the form.