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?
 
Yes, I'm cunfused because first you ask to have last item of listbox2 selected in listbox1, then you ask to have currently selected item in listbox2 selected in listbox1. Which is it?
 
This code selects the item in Listbox1 corresponding to the last item in Listbox2:
VB.NET:
If ListBox2.Items.Count > 0 Then
    ListBox1.SelectedItem = ListBox2.Items(ListBox2.Items.Count - 1)
End If
 
Hmmmm, well sorry it doesnt seem to work. The items in the first list box dont get highlighted.

So far I have this

Dim iCount As Integer
If lstItems.Items.Count > 0 Then
ListBox1.SelectedItem = lstItems.Items(lstItems.Items.Count - 1)
iCount = lstItems.Items.Count - 1
lstItems.Items.Remove(lstItems.Items(iCount))
 
You switched the order somewhat. Try these:
VB.NET:
ListBox1.SelectedItem = lstItems.Items(0) 'this is first item
VB.NET:
ListBox1.SelectedItem = lstItems.Items(1) 'this is second item
VB.NET:
ListBox1.SelectedItem = lstItems.Items(2) 'this is third item
 
Nope its still not working, maybe if you had an idea of what im trying to do it would make things a little bit easier. With the 2 list boxes and 2 buttons there is a text box shows the quantity of the amount of items in list box 1. When an item is selected from the first list box a random number is shown in the quantity, and if the add button is clicked the quantity minuses 1. As the following code is used:

VB.NET:
TextBox1.Text = TextBox1.Text - 1

When the remove button is pressed for the second listbox the opposite of the code above is used. This adds 1 to the quantity and this is where the problem occurs. For example if there are 5 different items selected and the remove button is selected 5 times the quantity will go high on just the 1 item. However if the last item in the listbox2 was selected from listbox1 then quantity would go higher to the right item. Hope you understood.
 
Frankly, I don't, isn't that just a consequence of the selection being wrong? Are you able to make a selection programmatically at all? Didn't the sample code in post 6 work? Or did you mean that nothing works? I have tested both codes from post 4 & 6 and they both work flawlessly in VS2003 for making the selection.
 
I know i might be asking for too much but is there any chance you could upload a sample program with the 2 list boxes and the rest, just so I could see how it works with yours and not mine and also where I am going wrong.
 
Sure, here it is:
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
    ListBox1.Items.Add("item1")
    ListBox1.Items.Add("item2")
    ListBox1.Items.Add("item3")
End Sub
 
Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ListBox2.Items.Add(ListBox1.SelectedItem)
End Sub
 
Private Sub RemoveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    ListBox2.Items.Remove(ListBox2.SelectedItem)
    If ListBox2.Items.Count > 0 Then
        ListBox1.SelectedItem = ListBox2.Items(ListBox2.Items.Count - 1)
    End If
End Sub
 
Private Sub TryToSelectAnythingButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    ListBox1.SelectedItem = ListBox1.Items(0) 'this is first item
End Sub
 
Thanks, I have a little bit of an idea where the problem is. Well the items are loaded in Listbox1 from a database, if you understand. I use one of the following coding with the Add button:

VB.NET:
        lstItems.Items.Add(TextBox3.Text)

OR

            lstItems.Items.Add(ListBox1.SelectedItem)
When using the second line the following line appears in the listbox 2, System.Data.DataRowView. You see I have to use the listbox2 to look up textbox3 to use that text as if i use listbox1 the System.Data.DataRowView line is shown. What are your suggestions?
 
Last edited:
What code where you using before to add item to listbox2 ?
 
I used the following:

VB.NET:
        TextBox1.Text = TextBox1.Text - 1
        If TextBox1.Text >= 0 Then
            Listbox2.Items.Add(TextBox3.Text)
        ElseIf TextBox1.Text <= 0 Then
            TextBox1.Text = 0
            MsgBox("No Items Left")
        End If
Textbox3 is where the text is (text coped from listbox1), and that text is carried to the listbox2

Just ignore textbox1 its the quantity of the items in listbox1 and the quantity gets minused every time the add button is selected.
 
Basically, the line of your coding that i use is

ListBox1.SelectedItem = lstItems.Items(lstItems.Items.Count - 1)

The aim of the above line is clear, the first list box must match the second list box. Is there a way in which I could make sure that the second list box matches a Text box? Because that text box will match the first listbox if you understand. Its just if i match the second listbox to the first listbox then that error appears in the second list box, the one on post 11.

EDIT

Or is there another method rather than ListBox2.Items.Add(ListBox1.SelectedItem) as this does copy the item onto the second listbox but instead copies that system line.
 
Last edited:
(text coped from listbox1)
I believe this is the key to your mystery, since this text is how you have to relate back to anyhow item however it is bound and whatever content is actually displayed. In other words, the text you display in listbox2 must somehow relate to where you found it in bound listbox1. There is no way to tell where you got that text other than for you to tell us.
 
Back
Top