Link to home
Start Free TrialLog in
Avatar of XK8ER
XK8ERFlag for United States of America

asked on

vb6 - add two items to listview

hello there,
I am using this code in old vb6 to add firstName and lastName to a listview.. how can I do the same thing on vb.net 2010?
Public Sub addFirstAndLast(mPath As String, LVW As ListView)
    Dim FSO As FileSystemObject, TS As TextStream, tabLine() As String, lItem As ListItem, j As Integer, sLine As String
 
    If Len(Dir(mPath)) > 0 And mPath <> "" Then
        LVW.ListItems.Clear
        Set FSO = New FileSystemObject
        Set TS = FSO.OpenTextFile(mPath, ForReading, False)
            While Not TS.AtEndOfStream
                sLine = TS.ReadLine
                If InStr(sLine, ":") Then
                    tabLine = Split(sLine, ":")
                    Set lItem = LVW.ListItems.Add(, , tabLine(0))
                    For j = 1 To UBound(tabLine)
                        lItem.ListSubItems.Add , , tabLine(j)
                    Next j
                End If
            Wend
        TS.Close
        Set TS = Nothing
        Set FSO = Nothing
    End If
End Sub

Open in new window

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Dim itmX As New ListViewItem
Dim subX As New ListViewItem.ListViewSubItem

itmX.Text = "test"
subX.Text = "sub"
itmX.SubItems.Add(subX)

' // Add item to ListView
lvStatus.Items.Add(itmX)
Avatar of XK8ER

ASKER

not really what im looking for!!
My code above creates a ListViewItem and also a Subitem. You can add as many Subitems as you need before adding it to the ListView object. This will create the ListView you require. I'll change it for you:

    Private Sub setListViewColumns()
        Dim clmX As ColumnHeader

        clmX = New ColumnHeader
        clmX.Text = "First Name"
        clmX.TextAlign = HorizontalAlignment.Left
        clmX.Width = 512
        lvStatus.Columns.Add(clmX)

        clmX = New ColumnHeader
        clmX.Text = "Surname"
        clmX.TextAlign = HorizontalAlignment.Left
        clmX.Width = 512
        lvStatus.Columns.Add(clmX)

        clmX = New ColumnHeader
        clmX.Text = "Mobile"
        clmX.TextAlign = HorizontalAlignment.Left
        clmX.Width = 512
        lvStatus.Columns.Add(clmX)
    End Sub

    Private Sub setListViewColumns()
      Dim itmX As New ListViewItem
      Dim subX As New ListViewItem.ListViewSubItem
      
      itmX.Text = "FirstName"
      
      subX = New ListViewItem.ListViewSubItem
      subX.Text = "Surname"
      itmX.SubItems.Add(subX)

      subX = New ListViewItem.ListViewSubItem
      subX.Text = "Mobile"
      itmX.SubItems.Add(subX)
      
      ' // Add item to ListView
      lvPerson.Items.Add(itmX)
    End Sub
Do you mind describing what you are looking for? You need to populate a multicolumn list view (details mode). right?
Something like...
    Public Sub addFirstAndLast(ByVal mPath As String, ByVal LVW As ListView)
        Dim line As String, tabLine() As String, lvi As ListViewItem
        If System.IO.File.Exists(mPath) Then
            Using sr As New System.IO.StreamReader(mPath)
                While Not sr.EndOfStream
                    line = sr.ReadLine
                    If line.Contains(":") Then
                        tabLine = line.Split(":")
                        lvi = LVW.Items.Add(tabLine(0))
                        For j As Integer = 1 To tabLine.GetUpperBound(0)
                            lvi.SubItems.Add(tabLine(j))
                        Next j
                    End If
                End While
            End Using
        End If
    End Sub

Open in new window

Avatar of XK8ER

ASKER

I have two 4 columns in the listview and the data text file that I am trying to import has this data

name1:last1:email:phone
name2:last2:email:phone
name3:last3:email:phone

when I use this code its adding email and phone.. it should not add them, just name and last!
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial