Simple Problem, Listboxes Help..

frankwhite

Well-known member
Joined
Nov 30, 2004
Messages
49
Programming Experience
Beginner
Hi, I was wondering if somebody would be able to help me out a little bit. On my form I Have 2 Listboxes and 2Buttons. The way in which this works is an item is selected from list box 1 and the button is selected for the item to be shown in the second list box up until now I am fine apart from the next part. I need help with some coding which will select whatever the last item is in the list box 2, to be same as list box 1. I know its confusing so I will try to explain it better:

Listbox 1 (All items available)- Item1, Item2, Item3, Item4, Item5

Button (Add item to listbox2)

Listbox 2 (Selected Items)- Item4, Item2, Item5, Item1.

Button (Remove item from Listbox2)

If the above scenario was in place, Item1 should be selected in Listbox 2, And when the button is clicked (Remove) The next item which is Item5 should be selected in Listbox1.

I need help with the coding to basically make the current item selected in listbox2, to be selected in Listbox1. If you are confused please ask me.

Any help please? Thanks?
 
Well that VB Program is linked to an Access database, and I linked the two together. Some of the values are from the database, for example:

ListBox1.DataSource = oDatatable
ListBox1.DisplayMember = "ItemDescription"
TextBox1.DataBindings.Add("Text", oDatatable, "CurrentItemPrice")
If lstItems.Items.Count > 0 Then
ListBox1.SelectedItem = lstItems.Items(lstItems.Items.Count - 1)
End If

Does the following help you?
 
Then this code should select the corresponding item in listbox1, where text of item in listbox2 matches the displayed text of an item in listbox1:
VB.NET:
If ListBox2.Items.Count > 0 Then
    Dim lastitemtext As String = ListBox2.Items(ListBox2.Items.Count - 1)
    For Each dr As Data.DataRowView In ListBox1.Items
        If dr("ItemDescription") = lastitemtext Then
            ListBox1.SelectedItem = dr
            Exit Sub
        End If
    Next
End If
 
Im Sorry about this but im a little bit confused, where should that piece of coding go? In the remove button? As that is where I have put it and it doesnt work.
 
That's too bad.
 
I got around the textbox part of the problem:

VB.NET:
lstItems.Items.Add(ListBox1.GetItemText(ListBox1.SelectedItem))

Im sure this has made it easier.
 
Back
Top