listviewvb6additionsubitem

how to add multiline item and subitem in listview in VB6?


i have data in textbox like this:

object

i want to split 3 type of that data to 3 coloums in listview. i know how to split that data by character using this type of code:

Private Sub Command1_Click()
Dim a As String
Dim b As String
Dim c As String

Dim i As Long
Dim sLines() As String
Dim sValues() As String
sLines() = Split(Text1.Text, vbCrLf)
For i = 0 To UBound(sLines)
   If sLines(i) > vbNullString Then ' skip for empty line
      sValues() = Split(sLines(i), ".")
      a = sValues(0) & vbCrLf
      b = sValues(1) & vbCrLf
      c = sValues(2) & vbCrLf
      Set List = ListView1.ListItems.Add(, , a)
      ListView1.ListItems.Add.SubItems(1) = b
      ListView1.ListItems.Add.SubItems(2) = c
   End If
Next i
End Sub

but the result is like this:

result

it isn't linier with the line. what happen with my code? is it wrong? please help me, thankyou


Solution

  • I'd suggest you try something like this, you could actually loop through the values array using a for i .. loop, but you get the gist!

    Private Sub Command1_Click()
    Dim a As String
    Dim b As String
    Dim c As String
    
    Dim i As Long
    Dim sLines() As String
    Dim sValues() As String
    Dim oItem As ListItem
    
    sLines() = Split(Text1.Text, vbCrLf)
    For i = 0 To UBound(sLines)
       If sLines(i) > vbNullString Then ' skip for empty line
          sValues() = Split(sLines(i), ".")
          a = sValues(0) & vbCrLf
          b = sValues(1) & vbCrLf
          c = sValues(2) & vbCrLf
    
          Set oItem = ListView1.ListItems.Add(, , sValues(0))
          Call oItem.ListSubItems.Add(, , sValues(1))
          Call oItem.ListSubItems.Add(, , sValues(2))
       End If
    Next i
    End Sub