Link to home
Start Free TrialLog in
Avatar of Richard Kreidl
Richard KreidlFlag for United States of America

asked on

Two Listboxes problem??

I have two ListBoxes in which I move items from Listbox1 to ListBox2 using DoubleClick on the item. I need to be able to check if the item is in Listbox2 then issue an error. But, my "If" statement below isn't working....

How would the "If" statement be coded???

Private Sub List1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles List1.DoubleClick
        If List1.SelectedIndex = List2.SelectedIndexCollection Then
            MsgBox("Server already exists")
     else
           LoadServer(List1.SelectedItem)
          CreateUMZ(List1.SelectedItem)
          Load_UMZBox1(List1.SelectedItem)
     End If
End Sub

thanks
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi rkckjk;

Try it this way.

    Private Sub List1_DoubleClick(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles List1.DoubleClick

        Dim AddItem As Boolean = True
        For Each List2Item As String In List2.Items
            If List1.SelectedItem Is List2Item Then
                AddItem = False
            End If
        Next

        If AddItem Then
            LoadServer(List1.SelectedItem)
            CreateUMZ(List1.SelectedItem)
            Load_UMZBox1(List1.SelectedItem)
        Else
            MsgBox("Server already exists")
        End If

    End Sub


Fernando
Avatar of Richard Kreidl

ASKER

Actually I just figured it out and it works:

Private Sub List1_DoubleClick(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles List1.DoubleClick
     If List2.Items().Contains(List1.SelectedItem) Then
             MsgBox("Server already exists in the UMZ listing")
         Else
             LoadServer(List1.SelectedItem)
             CreateUMZ(List1.SelectedItem)
             Load_UMZBox1(List1.SelectedItem)
     End If
End sub

Your code didn't work, but anyway maybe you could answer a followup question that's related to this??

I'm also trying to check if the items are in Listbox2 from a MultiSelect option on ListBox1..


So, if the user highlights several items in ListBox1 and trys to add them to ListBox2, I don't want to add them if they're already in ListBox2...


thanks
I using the following code for MultiSelect:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim b As String
        For i = 0 To List1.SelectedItems.Count - 1
            If List2.Items().Contains(b) Then
                MsgBox("Server already exists in the UMZ listing")
            Else
                b = List1.SelectedItems(i).ToString
                LoadServer(b)
                CreateUMZ(b)
                Load_UMZBox1(b)
            End If
        Next
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That works great!!! thanks
Not a problem always glad to help. ;=)