Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with listboxes

Status
Not open for further replies.

ilovevbsomuchimmad

Programmer
Feb 5, 2007
18
0
0
GB
Hi, I was wondering if anybody out there could help me out a little bit. I have two list boxes and 2 buttons, there are items in listbox 1 and the first button adds the items to the list box2, the second button only removes items from listbox2 and does not alter any of the items in list box1. As the items in listbox are linked to a data base the following coding was used with the add button:

Code:
Listbox2.Items.Add(ListBox1.GetItemText(ListBox1.SelectedItem))

What i really really need help with is If an item is selected from Listbox2 (To be removed), I JUST want the item to be selected in list box1, well I have the following coding:

Code:
If Listbox2.Items.Count > 0 Then ListBox1.SelectedItem = Listbox2.Items(Listbox2.Items.Count - 1) 
End If

I have changed the SelectedItems to FindStringExact and other methods as Listbox1 contains DataRowView objects and ListBox2 contains String objects but still none of them seem to be working. Please could you give me any suggestions?
 
Hi,
I am not sure if i understood right. Any way:

Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button2.Click

        If Me.ListBox2.SelectedIndex <> -1 Then
            Dim toFind As String = Me.ListBox2.Text
            Dim pos As Integer = Me.ListBox1.Items.IndexOf(toFind)

            If pos <> -1 Then
                Me.ListBox1.SelectedIndex = pos

            Else
                MessageBox.Show("ERROR - Item Not found")

            End If

        Else
            MessageBox.Show("ERROR - Please select an item for listbox2")

        End If

    End Sub
 
Hello Tipgiver, Sorry I dont think you understood what I was trying to say, I know it sounds confusing but let me break it down. I have 2 ListBox's, Basically there are items in Listbox1, and the user selects a Random item and clicks on the first button, this makes the selected Item appear in Listbox2. Now, In listbox2 Appear whatever the User selected from listbox1, There is a remove button and I know how to make the remove button work but that is not the help I need. What I want is when the user selects any random item from Listbox2, The same item in Listbox1 Gets Highlighted/Selected.

This is not difficult as the second piece of coding I have posted in my first post does this, However it does not work in this case, as the Items in listbox1 are retrived from a Database and are displayed as DataRowView objects. Hope this has made it much clearer and you understand.

Thanks
 
While I do not understand the logic of what you are trying to do (If you want a datarow set based on something in the first listbox, why use the second?), here is something that should help you down the path to what you need.

Code:
        Me.ListBox1.ClearSelected()
        Me.ListBox1.SelectedItem = Me.ListBox2.SelectedItem

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Hi mstrmage1768, I know it sounds stupid from where you are sitting but it is the best solution to what I am trying to achieve. Basically, the system I amtrying to build lets users see what type of items I have and the Quantities of the items. Different Items in Listbox1 have different Quantity numbers, when the first button is selected the Quantity goes Minus 1, until 0 where no more items get added to the listbox2. When the second button (Remove item from listbox2) is selected the Quantity goes 1 higher, However If all items are removed from Listbox2, the full Quantity will go to the last item that was added to the listbox2 and distributed equally to the correct item. With the coding what I am trying to do is, the last item in listbox2 matches the item in listbox 1, and from this the correct Quantity is returned to the item.

From using your coding, what it is doing is when the remove button is clicked the first item gets selected in Listbox1, not the matching item if you understand.
 
The fact that the first item is selected when you use the "remove" button makes perfect sense...You are removing an item from the control's array of items, and not giving it a new index to use, so it defaults to the base, which is 0 in most cases.

I think I forgot to mention the code I supplied was for the SelectedIndexChanged event of the second listbox. You previous post just before mine stated " What I want is when the user selects any random item from Listbox2, The same item in Listbox1 Gets Highlighted/Selected."

So if you place that code in the SelectedIndexChanged event of Listbox2...When a user clicks on an item in Listbox2, the same item should be selected in Listbox1.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top