|
 |
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.
|
Imports System.Collections |
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
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
|
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!
The front end ListboxVB2005.aspx page looks something like this:
<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>
|
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
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
|
Download the Full Working Version of this Project written with Visual Studio.NET VB.NET 2005 Here!
Looking for the C# 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!
|
|
|