Solved

Two Listboxes problem??

Posted on 2006-11-05
6
181 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:rkckjk
  • 3
  • 3
6 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17876817
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
0
 
LVL 2

Author Comment

by:rkckjk
ID: 17876850
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
0
 
LVL 2

Author Comment

by:rkckjk
ID: 17876855
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
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17876891
The first solution worked for me, anyway here is how to do the multiselected version. Jus move this line to where I placed it below.

    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
            b = List1.SelectedItems(i).ToString
            If List2.Items().Contains(b) Then
                MsgBox("Server already exists in the UMZ listing")
            Else
                LoadServer(b)
                CreateUMZ(b)
                Load_UMZBox1(b)
            End If
        Next

    End Sub

Fernando
0
 
LVL 2

Author Comment

by:rkckjk
ID: 17876899
That works great!!! thanks
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17876919
Not a problem always glad to help. ;=)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Allow User To Arrange Columns At Datagridview 3 28
C# Single Form 8 44
VB.NET - Error - Object not set in Module 4 35
VB.Net WebRequest Code from JSON curl 9 18
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question