Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to populate listview with data from text file ?

Status
Not open for further replies.

davidck88

Programmer
Jan 5, 2009
27
NL
Hi all i am using the following code to load text to two listbox. However, i want to change it so it loads data into listview. Could any one show me how this can be done?

I tried to use:
Code:
 ListView1.ListItems.Add strBuffArr(0)
but i got run time error 13 data mismatch !!

The data stored in text files are like this:

the power of love.mp3 c:\music\the power of love.mp3
the wall.mp3 c:\music\wind.mp3
......

I want song title and path be stored in seperate columns of listview.Hope you guys help me achive this task. Looking forward for reply.Thanks

Code:
Dim i As Long
Dim intLB As Long   'Is an index counter
Dim lngCtr As Long  'Represents the count of individual lines of data
Dim strOpenPathFile As String, strArr() As String, strBuffArr() As String
        
    With CommonDialog1
        .CancelError = True
        On Error GoTo CommDiaCancelled
        .Filter = "Text Files (*.txt)|*.txt|Word Doc (*.doc)|*.doc"
        .ShowOpen
    End With
    
    strOpenPathFile = CommonDialog1.FileName

    Open strOpenPathFile For Input As #1
        strArr = Split(Input(LOF(1), 1), vbCrLf)
    Close #1

    'Clear all the ListBoxs
    List1.Clear
    List2.Clear
     lngCtr = UBound(strArr)

    For intLB = 0 To lngCtr
    
CommDiaCancelled:
    If Err.Number = 32755 Then
        MsgBox "Cancel selected !"
        Exit Sub
    End If
        'Check for unwanted vbCrLf (which would yeild a "")
        If Len(strArr(intLB)) <> 0 Then

            'Split the Line of data
            strBuffArr = Split(strArr(intLB), vbTab)

            'Load the data into the ListBoxs

            List1.AddItem strBuffArr(0)
            List2.AddItem strBuffArr(1)
           
        End If
    Next
 
You're trying to add the string value into the integer Index parameter.

The parametrs for the Add method are: .Add(index, key, text, icon, smallIcon)

Have a try with:
Code:
List1.AddItem , , strBuffArr(0)
List2.AddItem , , strBuffArr(1)
Hope this helps

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
This should get you started:

Dim Item

LV1.View = lvwReport 'define type of list

'Add titles to each column header
LV1.ColumnHeaders.Add , , "Track Name", 2000 'this first column can't be justified
LV1.ColumnHeaders.Add , , "Location", 3000, 0 '0 = left justify

Set Item = LV1.ListItems.Add(, , strBuffArr(0))
Item.SubItems(1) = strBuffArr(1)

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top