moving items between two listboxes

A

ASPirant

Hi all,

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 Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click, Button2.Click

Dim btn As Button = CType(sender, Button)
Dim lst As ListBox

If btn Is Button1 Then
lst = ListBox1
Else
lst = ListBox2
End If

Dim obj As Object = lst.SelectedItem

If Not obj Is Nothing Then
lst.Items.Remove(obj)
If lst Is ListBox1 Then
ListBox2.Items.Add(obj)
Else
ListBox1.Items.Add(obj)
End If
End If
End Sub

----- end code -----
 

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,828
Messages
2,569,736
Members
45,518
Latest member
vlonesshirt

Latest Threads

Top