Using the ListBox Control with ASP.NET 4.0 and VB
To demonstrate how to work with the list box control, we will need to create a simple web form. For this example we will add two list boxes to the web form and add some buttons that will allow us to add items back and forth to the two list boxes. To begin, create a new ASP.NET Empty Web Site and:
- Right click the project in your solution explorer.
- Select add new item…
- Select a web form.
- Name it ‘Default.aspx’.
- Click add.
- Open Default.aspx up to design mode.
- Drag and drop a listbox onto the web form.
- Change the ID property to ‘lstAsset’.
- Change the SelectionMode property to ‘Multiple’.
- Add the following list items to the list box:
- Drag and drop a listbox onto the web form.
- Change the ID property to ‘lstSubordinateAsset’.
- Change the SelectionMode property to ‘Multiple’.
- Add the following list items to the list box:
- Add a break line.
- Drag and drop a button onto the web form.
- Change the Text property to ‘<<’.
- Drag and drop a button onto the web form.
- Change the Text property to ‘<’.
- Drag and drop a button onto the web form.
- Change the Text property to ‘>’.
- Drag and drop a button onto the web form.
- Change the Text property to ‘>>’.
Need help with cloud hosting? Try Server Intellect. We used them for our cloud hosting services and we are very happy with the results!
Once we have our form setup, we need to add the functionality for our buttons. But first, we need to create the array lists that will hold our data. To do this, open Default.aspx.vb up for editing and add in the following code:
The array lists for our list boxes.
Private lasset As New ArrayList()
Private lsubordinate As New ArrayList()
Private Shared UpdateList As New ArrayList()
The first button will move all of the items from our second list box to our first list box. To do this, open Default.aspx up to design mode and double click Button1. Then, add the following code to that event method:
The Button1_Click event method.
While lstSubordinateAsset.Items.Count <> 0
Dim i As Integer
For i = 0 To lstSubordinateAsset.Items.Count - 1
If Not lsubordinate.Contains(lstSubordinateAsset.Items(i)) Then
lsubordinate.Add(lstSubordinateAsset.Items(i))
End If
Next i
For i = 0 To lsubordinate.Count - 1
If Not lstAsset.Items.Contains(CType(lsubordinate(i), ListItem)) Then
lstAsset.Items.Add(CType(lsubordinate(i), ListItem))
End If
lstSubordinateAsset.Items.Remove(CType(lsubordinate(i), ListItem))
UpdateList.Add(lsubordinate(i))
Next i
End While
Next, open Default.aspx up to design mode and double click Button2. Then, add the following code to that event method:
The Button2_Click event method.
If Not (lstSubordinateAsset.SelectedItem Is Nothing) Then
Dim i As Integer
For i = 0 To lstSubordinateAsset.Items.Count - 1
If lstSubordinateAsset.Items(i).Selected Then
If Not lsubordinate.Contains(lstSubordinateAsset.Items(i)) Then
lsubordinate.Add(lstSubordinateAsset.Items(i))
End If
End If
Next i
For i = 0 To lsubordinate.Count - 1
If Not lstAsset.Items.Contains(CType(lsubordinate(i), ListItem)) Then
lstAsset.Items.Add(CType(lsubordinate(i), ListItem))
End If
lstSubordinateAsset.Items.Remove(CType(lsubordinate(i), ListItem))
UpdateList.Add(lsubordinate(i))
Next i
End If
We used over 10 web hosting companies before we found Server Intellect. Our new server with cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.
This will take the selected items from our second list box and add them to our first list box. Next, open Default.aspx up to design mode and double click Button3. Then, add the following code to that event method:
The Button3_Click event method.
If lstAsset.SelectedIndex >= 0 Then
Dim i As Integer
For i = 0 To lstAsset.Items.Count - 1
If lstAsset.Items(i).Selected Then
If Not lasset.Contains(lstAsset.Items(i)) Then
lasset.Add(lstAsset.Items(i))
End If
End If
Next i
For i = 0 To lasset.Count - 1
If Not lstSubordinateAsset.Items.Contains(CType(lasset(i), ListItem)) Then
lstSubordinateAsset.Items.Add(CType(lasset(i), ListItem))
End If
lstAsset.Items.Remove(CType(lasset(i), ListItem))
Next i
End If
This will add the selected items from our first list box to our second list box. Next, open Default.aspx up to design mode and double click Button4. Then, add the following code to that event method:
The Button4_Click event method.
While lstAsset.Items.Count <> 0
Dim i As Integer
For i = 0 To lstAsset.Items.Count - 1
If Not lasset.Contains(lstAsset.Items(i)) Then
lasset.Add(lstAsset.Items(i))
End If
Next i
For i = 0 To lasset.Count - 1
If Not lstSubordinateAsset.Items.Contains(CType(lasset(i), ListItem)) Then
lstSubordinateAsset.Items.Add(CType(lasset(i), ListItem))
End If
lstAsset.Items.Remove(CType(lasset(i), ListItem))
Next i
End While
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
This moves all of the items from our first list box to our second list box.
Testing
To test this out load up the web site. Use the four buttons to move data to and from our list boxes. This demonstrates how you can easily use the list box control.
Use ListBox asp4 vb
