locked
Cannot add or insert the item 'xxxxx' in more than one place.

    Question

  • I have an application that manages orders in a system, it has multiple tabs e.g. "Summary" (New and Processing Orders), "New" (Only New), "Processing" (Only Processing) and "Completed" (Only completed).
    These are identified by an integer, (0, 1 or 2).
    Each tab contains a listview (lvSummary, lvNew, lvProcessing, lvCompleted)
    I have a function that takes an order object and puts it into the appropriate list view, however I want to put new and processing orders into the summary list view.
    When i attempt this i get this message:
    Cannot add or insert the item 'xxxxx' in more than one place. You must first remove it from its current location or clone it.
    Parameter name: item

    Here is my code:
            Dim lvItem As ListViewItem
            lvItem = New ListViewItem
    
            With OrderObject
                lvItem.Text = .ID
                lvItem.SubItems.Add(.Customer.FirstName & " " & .Customer.LastName)
                lvItem.SubItems.Add(.DateOrdered)
                lvItem.SubItems.Add(.Time)
                lvItem.SubItems.Add(.Total)
                lvItem.SubItems.Add(.OrderStatus.ToString)
                lvItem.SubItems.Add(.Comments)
    
                If .OrderStatus = Order.Status.Processing Then
                    lvItem.BackColor = Color.LimeGreen
    
                    frmMain.lvProcessing.Items.Add(lvItem)
                End If
                If .OrderStatus = Order.Status.Completed Then
                    lvItem.BackColor = Color.SkyBlue
    
                    frmMain.lvCompleted.Items.Add(lvItem)
                End If
            End With

    What am i doing wrong?
    I've tried this, but it doesn't work (Or i'm doing it wrong)
    Dim NewListViewItem As New ListViewItem
    
    NewListViewItem = lvItem
    
    frmMain.lvNew.Items.Add(NewListViewItem)
    frmMain.lvSummary.Items.Add(lvItem)



    • Edited by interwave Saturday, February 04, 2012 6:38 PM (Had more info to include + crap formatting)
    Saturday, February 04, 2012 6:35 PM

Answers

  • it's telling you to remove it or clone it. If you don't want to remove it, did you try cloning it?

    see this thread for an example:

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/95a612b9-e8b3-4133-b20d-07b85ef71a2b

    Saturday, February 04, 2012 8:17 PM
  • You do not create a new listview item just by changing some of the item values.  You need to create a new reference to a listviewitem object.  This line
            lvItem = New ListViewItem

    creates a new object, but it probably still refers to an existing item in the listview.  You need to ensure that the variable used to refer to your new item has a scope that is within the routine where you are adding it to the listview.  I suspect the variable actually has much wider scope, which means that you are always dealing with the same listview item and the same listview item cannot be referenced from more than one place in the listview.   You havent included quite enough code to identify whether you are really creating a new reference to your new item, or are trying to re-use an existing reference to that item, but the error message you are receiving indicates the latter.

    • Edited by Acamar Saturday, February 04, 2012 9:09 PM sp
    • Marked as answer by Shanks ZenModerator Wednesday, February 15, 2012 7:33 AM
    Saturday, February 04, 2012 9:03 PM

All replies

  • it's telling you to remove it or clone it. If you don't want to remove it, did you try cloning it?

    see this thread for an example:

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/95a612b9-e8b3-4133-b20d-07b85ef71a2b

    Saturday, February 04, 2012 8:17 PM
  • You do not create a new listview item just by changing some of the item values.  You need to create a new reference to a listviewitem object.  This line
            lvItem = New ListViewItem

    creates a new object, but it probably still refers to an existing item in the listview.  You need to ensure that the variable used to refer to your new item has a scope that is within the routine where you are adding it to the listview.  I suspect the variable actually has much wider scope, which means that you are always dealing with the same listview item and the same listview item cannot be referenced from more than one place in the listview.   You havent included quite enough code to identify whether you are really creating a new reference to your new item, or are trying to re-use an existing reference to that item, but the error message you are receiving indicates the latter.

    • Edited by Acamar Saturday, February 04, 2012 9:09 PM sp
    • Marked as answer by Shanks ZenModerator Wednesday, February 15, 2012 7:33 AM
    Saturday, February 04, 2012 9:03 PM
  • In the code you posted, you refer to a "frmMain".  This is highly suspicious of poor programming practice.  If "frmMain is the main form of the app, it would never be accessed by its name.  It would typically be accessed through an event.  If the class containing the code shows "frmMain", then the code is OK.
    Saturday, February 04, 2012 9:21 PM