DotNet Tutorials

Server Intellect

 Working with List box in ASP.NET 2.0 and VB.NET

The ListBox control enables you to display a list of items to the user that the user can select by clicking. A ListBox control can provide single or multiple selections using the SelectionMode property. This tutorial show you how to choose multi items and move them to the other Listbox. The ListBox control enables you to display a list of items to the user that the user can select by clicking. A ListBox control can provide single or multiple selections using the SelectionMode property. The ListBox also provides the MultiColumn property to enable the display of items in columns instead of a straight vertical list of items. This allows the control to display more visible items and prevents the need for the user to scroll to an item. This tutorial show you how to move items of ListBox from one to the other.

First, you will need to import the using System.Collections namespace. The System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries.

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

Imports System.Collections

We transfer items of  ListBox from one ListBox to another by following four buttons.

Public Sub AddBtn_Click(Src As [Object], E As EventArgs)
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
End Sub

Public Sub AddAllBtn_Click(Src As [Object], E As EventArgs)
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
End Sub

Public Sub RemoveBtn_Click(Src As [Object], E As EventArgs)
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
End Sub

Public Sub RemoveAllBtn_Click(Src As [Object], E As EventArgs)
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
End Sub

The front end  ListboxVB2005.aspx page looks something like this:

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

<table style="width: 368px" align="center">
<tr>
<td style="width: 104px" align="center">
Available Fonts
</td>
<td style="width: 133px">
<!-- Filler -->
</td>
<td style="width: 110px">
Installed Fonts
</td>
</tr>
<tr>
<td style="width: 104px" align="center">
<asp:listbox id="lstAsset" width="100px" runat=server SelectionMode="Multiple">
<asp:listitem>Roman</asp:listitem>
<asp:listitem>Arial Black</asp:listitem>
<asp:listitem>Garamond</asp:listitem>
<asp:listitem>Somona</asp:listitem>
<asp:listitem>Symbol</asp:listitem>
</asp:listbox>
</td>
<td style="width: 133px">
<!-- Filler -->
</td>
<td style="width: 110px">
<asp:listbox id="lstSubordinateAsset" width="100px" runat=server SelectionMode="Multiple">
<asp:listitem>Times</asp:listitem>
<asp:listitem>Helvetica</asp:listitem>
<asp:listitem>Arial</asp:listitem>
</asp:listbox>
</td>
</tr>
<tr>
<td style="width: 104px">
<!-- Filler -->
</td>
<td style="width: 133px" align="center">
<asp:button ID="Button1" text="<<" OnClick="RemoveAllBtn_Click" runat=server Width="30px"/>
<asp:button ID="Button2" text="<" OnClick="RemoveBtn_Click" runat=server Width="26px"/>
<asp:button ID="Button3" text=">" OnClick="AddBtn_Click" runat=server Width="29px"/>
<asp:button ID="Button4" text=">>" OnClick="AddAllBtn_Click" runat=server Width="33px"/></td>
<td style="width: 110px">
<!-- Filler -->
</td>
</tr>
</table>

The flow for the code behind page is as follows.

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections

Class _Default
Private lasset As New ArrayList()
Private lsubordinate As New ArrayList()
Private Shared UpdateList As New ArrayList()

Protected Sub Page_Load(sender As Object, e As EventArgs)

End Sub

Public Sub AddBtn_Click(Src As [Object], E As EventArgs)
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
End Sub

Public Sub AddAllBtn_Click(Src As [Object], E As EventArgs)
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
End Sub

Public Sub RemoveBtn_Click(Src As [Object], E As EventArgs)
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
End Sub

Public Sub RemoveAllBtn_Click(Src As [Object], E As EventArgs)
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
End Sub
End Class


Looking for more ASP.NET Tutorials? Click Here!

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.


Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect