DotNetWebTutor.com                                                    Link to Us

Lists
vb listbox control
The Listbox control
As the items in the list grows a scrollbar appears
Items property is used to add items
Example:

  • Start a new project
  • Add a listbox
  • Click the ellipsis next to the items property and type in each item line by line
  • You can also use the smart tag to add to the listbox

String Collection Editor
Items is a collection
A collection allows you to easily add and remove data

  • Add a textbox and button
  • Add this code to the button

listBox1.Items.Add(textBox1.Text)
textBox1.Text = ""
The Add method of items lets you add items to your listbox

  • Test it

items
Items
You can get at a certain number in the collection
textBox1.Text = listBox1.Items(2)
You can test this by adding another button and adding this code
Gets the third item from the list
The first item in the list is 0 and increment from there
selecteditem
SelectedItem
You can retrieve the currently selected item from the SelectedItem property
Example:

  • Add a label(label1)
  • Double click the listbox to add code for the SelectedIndexChanged event
  • Add this code:

label1.Text = listBox1.SelectedItem.ToStringget the number in the list
To get the number in the list:
Dim mycount as integer=listbox1.items.count
Example:

  • Add another button and add this code

MessageBox.Show("There are " + listBox1.Items.Count.ToString + " items")use a for/each
Use a for/each statement to loop through the items
Add another listbox(listbox2)
Add a button and add this code to the click event:
For each myitem as string in listBox1.Items         
listBox2.Items.Add(myitem)
Next
removing items
Removing items
You can remove an item by matching the string name
listBox1.Items.Remove("Pete")
Or you can remove an item at a specific location in the collection
listBox1.Items.RemoveAt(3)
To remove a selected item do the following example

  • Add another button and label it delete

if listBox1.SelectedIndex <> -1 then
listBox1.Items.Remove(listBox1.SelectedItem.ToString)
end if
This will cause your previous code to crash from selectedIndexchanged
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
label1.Text = ListBox1.SelectedIndex.ToString
End Sub

  • Highlight the code in the event handler and click the comment button:

comment out button in visual studio
checkboxlist control
CheckedListBox Control
Works like the listbox except each item has a checkbox in front of it
To check for a checkedlistbox item
Example

  • Start a new project
  • Add a checkedlistbox
    • Add some names to the Items collection like in the listbox
  • Add a listbox
  • Add a button
  • Add this code to the button

For Each varname As String In CheckedListBox1.CheckedItems
ListBox1.Items.Add(varname.ToString())
Next

  • Set the checkonClick() property to true or the user will have to click an item twice

combobox control
ComboBox Control
A combobox is a textbox with a list attached
3 possible styles of DropDownStyles property

  • Simple
  • Dropdown
  • Dropdownlist

Simple
Selecting something in the list puts it in the textbox
DropDownList style
User cannot type in the textbox, they have to click the dropdown button and select from the list and then it gets displayed in the textbox
DropDown
Same as dropdownlist but the user can type in the textbox
text property can be used to retrieve selected value
Selecteditem and selectedIndex works like the listbox
vb list controls