Richard Kreidl
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.SelectedIndexCollect ion Then
MsgBox("Server already exists")
else
LoadServer(List1.SelectedI tem)
CreateUMZ(List1.SelectedIt em)
Load_UMZBox1(List1.Selecte dItem)
End If
End Sub
thanks
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.SelectedIndexCollect
MsgBox("Server already exists")
else
LoadServer(List1.SelectedI
CreateUMZ(List1.SelectedIt
Load_UMZBox1(List1.Selecte
End If
End Sub
thanks
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(Lis t1.Selecte dItem) Then
MsgBox("Server already exists in the UMZ listing")
Else
LoadServer(List1.SelectedI tem)
CreateUMZ(List1.SelectedIt em)
Load_UMZBox1(List1.Selecte dItem)
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
Private Sub List1_DoubleClick(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles List1.DoubleClick
If List2.Items().Contains(Lis
MsgBox("Server already exists in the UMZ listing")
Else
LoadServer(List1.SelectedI
CreateUMZ(List1.SelectedIt
Load_UMZBox1(List1.Selecte
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
ASKER
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).ToS tring
LoadServer(b)
CreateUMZ(b)
Load_UMZBox1(b)
End If
Next
End Sub
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).ToS
LoadServer(b)
CreateUMZ(b)
Load_UMZBox1(b)
End If
Next
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That works great!!! thanks
Not a problem always glad to help. ;=)
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.SelectedI
CreateUMZ(List1.SelectedIt
Load_UMZBox1(List1.Selecte
Else
MsgBox("Server already exists")
End If
End Sub
Fernando