Results 1 to 3 of 3

Thread: [RESOLVED] adding two items to a listview in columns in vb 2010 express

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    28

    Resolved [RESOLVED] adding two items to a listview in columns in vb 2010 express

    Hello All;

    I have a simple program in vb 2010 express that I am having trouble with.

    The form contains a button and a listview.

    When the button is clicked, I want the code to open a random access file and get words from the file.
    Then, determine the length of the word and add the word to one column of the listview and the length to
    another. So far this is what I have in code:

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim tmpInt1, tmpInt2 As Integer
    Dim tmpString1, tmpString2 As String
    Dim wrditem As New ListViewItem
    Dim FileNum As Integer
    
    ' tmpInt1 = number of word entries in the file
    ' the words start at index 3, index 2 is another integer
    
    FileNum = FreeFile()
    FileOpen(FileNum, "C:\[somepath]\[some random access file]", OpenMode.Random, OpenAccess.Read, OpenShare.LockRead)
    FileGet(FileNum, tmpInt1, 1)
    
            For x As Integer = 3 To (tmpInt1 + 2)
                FileGet(FileNum, tmpString1, x)
                If tmpString1 <> "" Then
                    tmpString2 = Trim(Mid(tmpString1, 1, 15))
                    tmpInt2 = Len(Trim(tmpString2))
                    wrditem.Text = tmpString2
                    wrditem.SubItems.Add(Str(tmpInt2))
                    ListView1.Items.Add(wrditem)
                End If
            Next
    
    FileClose(FileNum)
    
    End Sub
    This is the error I get on the second word pulled in from the file: "Argument Exception was unhandled - cannot add or insert the item 'aaronites' in more than one place. You must first remove it from its current location or clone it. Parameter name: item"

    Can anyone tell me what I am doing wrong. This word is only in the list once.

    Thanks

  2. #2
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: adding two items to a listview in columns in vb 2010 express

    Hey GMTireman. Welcome to the forum.

    Your problem is that you only create the one ListViewItem (wrditem) which you try to add over and over to the ListView, which is something you're not allowed to do. You need to create a New ListViewItem every time you add one.

    Your simplest solution would be to move the Dim wrditem As New ListViewItem line into the If..End IF block thus:
    Code:
    If tmpString1 <> "" Then
        tmpString2 = Trim(Mid(tmpString1, 1, 15))
        tmpInt2 = Len(Trim(tmpString2))
        Dim wrditem As New ListViewItem
        wrditem.Text = tmpString2
        wrditem.SubItems.Add(Str(tmpInt2))
        ListView1.Items.Add(wrditem)
    End If

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    28

    Re: adding two items to a listview in columns in vb 2010 express

    Thanks, did just that works GREAT! I sincerely appreciate the 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
  •  



Click Here to Expand Forum to Full Width