Move selected items from a listView to another
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Move selected items from a listView to another

  1. #1
    Join Date
    Jun 2003
    Posts
    76

    Question Move selected items from a listView to another

    Hi all,

    I have two listViews (QuantityListView and SlectedQuantityListView). I want to copy items from the first listView(QuantityListView) and put them in the second one (SelectedQuantityListView). But I have to check that the item to be put in the second listeView does not exist already in this second listView (to avoid duplication). The following code works but only if I pick two items, if i pick more than two it duplicates some items. Could anyone check what 's wrong with this code and provide some feedback

    Thank you!

    Code:
    Private Sub QuantityAdd()
            Try
                If QuantityListView.SelectedItems.Count = 0 Then
                    MsgBox("Select a quantity name frist")
                    Exit Sub
                End If
                Dim ele As ListViewItem
                Dim i, j As Integer
                For j = 0 To QuantityListView.SelectedItems.Count - 1
                    If SelectedQuantityListView.Items.Count <> 0 Then
                        For i = 0 To SelectedQuantityListView.Items.Count - 1
                            If  QuantityListView.SelectedItems(j).Text =SelectedQuantityListView.Items(i).Text  Then
                                'Exit For
                                MsgBox("already exists!")
                            Else
                                ele = SelectedQuantityListView.Items.Add(QuantityListView.SelectedItems(j).Text)
                            End If
                        Next
                    Else
                            ele = SelectedQuantityListView.Items.Add(QuantityListView.SelectedItems(j).Text)
                    End If
                    'Next
                Next
                    Catch ex As Exception
                mainapp.ShowError(ex, "")
            Finally
            End Try
        End Sub

  2. #2
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,891
    It looks like this is the bad code:
    Code:
                        For i = 0 To SelectedQuantityListView.Items.Count - 1
                            If  QuantityListView.SelectedItems(j).Text =SelectedQuantityListView.Items(i).Text  Then
                                'Exit For
                                MsgBox("already exists!")
                            Else
                                ele = SelectedQuantityListView.Items.Add(QuantityListView.SelectedItems(j).Text)
                            End If
                        Next
    You are only actually checking the first item, not all of them. You can do something like this:

    The follow is notepad formatting, so it may not look pretty...
    Code:
    Private Sub QuantityAdd()
            Try
                If QuantityListView.SelectedItems.Count = 0 Then
                    MsgBox("Select a quantity name frist")
                    Exit Sub
                End If
                Dim ele As ListViewItem
                Dim bExists as Boolean
                Dim i, j As Integer
                For j = 0 To QuantityListView.SelectedItems.Count - 1
                    If SelectedQuantityListView.Items.Count <> 0 Then
                        For i = 0 To SelectedQuantityListView.Items.Count - 1
                            If  QuantityListView.SelectedItems(j).Text =SelectedQuantityListView.Items(i).Text  Then 
                               bExists = True
                               Exit For
                            End If
                        Next
       		    
    		    If Not bExists then 
    	    	    	    ele = SelectedQuantityListView.Items.Add(QuantityListView.SelectedItems(j).Text)
    	    	    Else
    	    	    	    bExists = False
    	    	    End If
    
                    Else
                            ele = SelectedQuantityListView.Items.Add(QuantityListView.SelectedItems(j).Text)
                    End If
                    'Next
                Next
                    Catch ex As Exception
                mainapp.ShowError(ex, "")
            Finally
            End Try
        End Sub
    Last edited by Craig Gemmill; June 3rd, 2004 at 10:31 PM.
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow threads, so if you have a secondary question, message me.

  3. #3
    Join Date
    Jun 2003
    Posts
    76
    That's correct!, thank you!
    Last edited by Katunigan; June 4th, 2004 at 05:20 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center