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

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.

  4. #4
    Join Date
    Mar 2015
    Posts
    2

    Re: Move selected items from a listView to another

    Hello, I have a similar situation except for I will not "Manually" select my Rows. Please Advise

    'Also Let Me Note - I will actually only want to move an item from lvOriginalList if the PersonAge is (lets say) '21

    'I have a ListView named
    ' lvOriginalList

    'It has the Following Columns
    ' IDNumber
    ' PersonName
    ' PersonAge

    'I have a Second ListView named
    ' lvRemovedList

    'It has the Following Columns
    ' IDNumber
    ' PersonName
    ' PersonAge

    '************************************************

    'What I am attempting to do is read a row out of the first listview
    'Compare the IDNUmber, PersonName and PersonAge from out of it (lvOriginalList)
    'And If the Information is not present in the second ListView (lvRemoved) then Move it to it
    'And then Remove that Row from the First one (lvOriginalList)
    '*****************************************************************************************************

    'What I have tried:
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Populate lvOriginalList with Test Data
    Dim FileNum As Integer = FreeFile()
    Dim lvOriginalListCounter As Integer = 0

    FileOpen(FileNum, "d:\testdata\alphadata.txt", OpenMode.Input)
    lvOriginalList.Items.Clear()

    Do While Not EOF(FileNum)

    Dim OriginalIDNumber As String = ""
    Dim OriginalPersonName As String = ""
    Dim OriginalPersonAge As String = ""

    OriginalIDNumber = LineInput(FileNum)
    OriginalPersonName = LineInput(FileNum)
    OriginalPersonAge = LineInput(FileNum)

    lvOriginalList.Items.Add(OriginalIDNumber, lvOriginalListCounter)
    lvOriginalList.Items(lvOriginalListCounter).SubItems.Add(OriginalPersonName)
    lvOriginalList.Items(lvOriginalListCounter).SubItems.Add(OriginalPersonAge)

    lvOriginalListCounter = lvOriginalListCounter + 1

    Loop
    FileClose(FileNum)


    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim OriginalIDNumber As String = ""
    Dim OriginalPersonName As String = ""
    Dim OriginalPersonAge As String = ""

    Dim CompareIDNumber As String = ""
    Dim ComparePersonName As String = ""
    Dim ComparePersonAge As String = ""

    Dim NumlvRemovedListItems As Integer = 0

    Dim CompareCounter As Integer = 0

    For Each item As ListViewItem In Me.lvOriginalList.Items
    '1 - Get A Row Out of lvOriginalList
    OriginalIDNumber = item.Text
    OriginalPersonName = item.SubItems.Item(1).Text
    OriginalPersonAge = item.SubItems.Item(2).Text

    '2 - Check To See If That Person Is Already in lvRemovedList
    'Have to check to see if the lvRemovedList list Box is Empty

    NumlvRemovedListItems = lvRemovedList.Items.Count

    If NumlvRemovedListItems = 0 Then
    'the lvRemovedList listbox is empty
    'so add That Person as the first entry into the list box (lvRemovedList)

    lvRemovedList.Items.Add(OriginalIDNumber, NumlvRemovedListItems)
    lvRemovedList.Items(NumlvRemovedListItems).SubItems.Add(OriginalPersonName)
    lvRemovedList.Items(NumlvRemovedListItems).SubItems.Add(OriginalPersonAge)

    'then remove the player from the lvPlayerList listbox
    item.Remove()

    Else
    For Each itemb As ListViewItem In Me.lvRemovedList.Items
    CompareIDNumber = item.Text
    ComparePersonName = item.SubItems.Item(1).Text
    ComparePersonAge = item.SubItems.Item(2).Text

    If OriginalPersonName = ComparePersonName Then
    'do nothing that persons name is already in the lvRemovedList list
    Else
    'do this because the player is not already in a matchin the lvRemovedList list
    lvRemovedList.Items.Add(CompareIDNumber, CompareCounter)
    lvRemovedList.Items(CompareCounter).SubItems.Add(ComparePersonName)
    lvRemovedList.Items(CompareCounter).SubItems.Add(ComparePersonAge)
    End If
    Next
    End If
    CompareCounter = CompareCounter + 1
    Next
    End Sub
    End Class
    'So what Am I doing Wrong

    'It is only Removing the First entry out of lvOriginalList (but at least the remaining data is still there and the empty Row is removed)
    'And only That first Entry is Appearing in lvRemovedList

    'Please Help

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




HTML5 Development Center