Transfer between two listboxes

A

ASPirant

This is a repost of my original question, but this time maybe with
more clarity which hopefully will elicit some helpful response.

What I have is as follows:
A drop-down box which when an item is selected, populates ListBox1. I
then want to select an item ( or more) from ListBox1 to ListBox2 ( and
vice versa).

The problem I am trying to resolve currently, however, only transfers
the item at the top of ListBox1 and not the one that I actually select
which may be a few items below that. In summary, it ignores my
selection and simply moves the the item at the top of the list! This is
very annoying and I can't see what it is that I need to do to respond
to my selection.

I know that SelectedItem always points to the first item in the list.
How do I identify the item that is actually selected??

Any ideas? See partial code below.

********** start code *******************
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click

Dim li As ListItem
Dim strTest As String



For Each li In ListBox1.Items
If li.Selected = True Then
ListBox2.Items.Add(li.Text)
End If
Next

Dim counter As Integer
For counter = (ListBox1.Items.Count - 1) To 0 Step -1
If ListBox1.Items(counter).Selected = True Then
ListBox1.Items.RemoveAt(counter)
End If
Next

End Sub
********** end code ********************

I know that this code works if ListBox1 is populated manually as the
page is loaded by code such as this:
Sub Page_Load(Source as Object, E as EventArgs)
If Not Page.IsPostBack Then
Dim intFirst As Integer
For intFirst = 1 To 10
ListBox1.Items.Add("Item " & intFirst)
Next
End If
End Sub

But this is not how my listbox is populated, but in a manner I have
described above. I would have thought that the manner in which it is
populated would not matter, but maybe it does?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top