Moving ListItems From A ComboBox / DropDownList To A ListBox
Refresh this page to reset the form
How to move a ListItem from a DropDownList / ComboBox control to a ListBox control. This code removes selected items from the DropDownList / ComboBox as they are added to the ListBox.
Page Code
<asp:DropDownList runat="server" ID="cbxCountries" AutoPostBack="true">
<asp:ListItem Text="Select ..." Value="" />
<asp:ListItem Value="USA" />
<asp:ListItem Value="Canada" />
<asp:ListItem Value="Mexico" />
<asp:ListItem Value="Guatemala" />
<asp:ListItem Value="Belize" />
<asp:ListItem Value="El Salvador" />
<asp:ListItem Value="Honduras" />
<asp:ListItem Value="Nicaragua" />
<asp:ListItem Value="Costa Rica" />
<asp:ListItem Value="Panama" />
</asp:DropDownList>
<asp:ListBox runat="server" ID="lstCountries" Rows="10" />
Code Behind
Sub cbxCountries_selectedIndexChanged(ByVal Sender As Object, ByVal E As EventArgs) Handles cbxCountries.SelectedIndexChanged
If Not String.IsNullOrEmpty(cbxCountries.SelectedValue) Then
'reset selected index for listbox; this is required for adding new listitems
lstCountries.SelectedIndex = -1
'ASP.NET version: add selected listitem to listbox
Dim lsiSelected As ListItem = cbxCountries.Items(cbxCountries.SelectedIndex)
'Windows Forms version: add selected listitem to listbox
'Dim lsiSelected As Object = cbxCountries.Items(cbxCountries.SelectedIndex)
lstCountries.Items.Add(lsiSelected)
'remove selected from combobox
cbxCountries.Items.RemoveAt(cbxCountries.SelectedIndex)
End If
End Sub