Results 1 to 10 of 10

Thread: add list to listview

  1. #1

    Thread Starter
    Lively Member Scorpionz's Avatar
    Join Date
    Feb 1999
    Location
    lafayette/kaplan, louisiana, usa
    Posts
    72

    Exclamation

    ok, say i have a list of items i'd like to add to my multicolumn (2 columns) listview:

    columna1
    columna2
    columnb1
    columnb2
    columnc1
    columnc2
    columnd1
    columnd2

    how would i do that... using something like a for next statement?

  2. #2
    Hyperactive Member tumblingdown's Avatar
    Join Date
    Mar 2000
    Posts
    362
    Clarify - you have a two column listview, and you wish to add eight more columns. Correct?

    The general syntax is ...

    ListView.ColumnsHeaders.Add (Index, Key, Text, Width, Alignment, Icon)

    I'm sure this is not what your asking for tho ;-)



    td.
    "One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig


    tumblingdown@hotmail.com

    "but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.

  3. #3

    Thread Starter
    Lively Member Scorpionz's Avatar
    Join Date
    Feb 1999
    Location
    lafayette/kaplan, louisiana, usa
    Posts
    72
    no, i wish to split the text between the 2 columns

    ex:

    i have this to split:

    columna1
    columna2
    columnb1
    columnb2
    columnc1
    columnc2
    columnd1
    columnd2

    and would like to have it like this in my listview:

    columna1 columna2
    columnb1 columnb2
    columnc1 columnc2
    columnd1 columnd2


  4. #4
    Hyperactive Member tumblingdown's Avatar
    Join Date
    Mar 2000
    Posts
    362
    oh, ok.

    Basically your'e adding Items and each item has one Sub-Item.

    General syntax is...

    ListView1.ListItems.Add(Index,Key,Text,Icon, _
    SmallIcon) as MSComctlLib.ListItem

    and a ListItem has a property SubItems(Index) as string.

    There are as many SubItems as there are columns in the ListView (remember you only see the SubItems in lvwReport view)

    so... (assuming you have two columns)


    Dim itm As MSComctlLib.ListItem

    ListView1.view = lvwReport
    set itm = ListView1.ListItems.Add (,,'Item X')
    if not (itm is nothing) then itm.SubItems(1) = 'Item Y'



    td.







    "One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig


    tumblingdown@hotmail.com

    "but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.

  5. #5

    Thread Starter
    Lively Member Scorpionz's Avatar
    Join Date
    Feb 1999
    Location
    lafayette/kaplan, louisiana, usa
    Posts
    72
    yes, all this i am aware of =P

    what i'm getting at is, i have a list of items from a parsed string in single file. what i want to do with that list is loop it to where it adds the first entry under the first column, second entry under second column, third entry under first column, fourth entry under second column and so on until my list is fully queried into the listview...

  6. #6
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Code:
    Dim stringParse() As String
    
    stringParse = Split(variable-containing-your-data,vbCrlf)
    For X = 0 to UBound(stringParse)
        If Int(X / 2) = X / 2 Then
            'Place Code Here to add stringParse(X) to ColumnA
            Else
            'Place Code Here to add stringParse(X) to ColumnB
        End If
    Next X
    ReDim stringParse(0)
    This should take a variable that contains your text file and split it into array elements using Crlf as a delimiter. Since arrays are a 0 based list (by default) then checking the int of X / 2 to the actual X / 2 will determine if it's odd or even. Due to the 0 based array, all even numbers in the array would fall under columna, while all odd numbers would fall under columnb.

    Array(0) = Line 1
    Array(1) = Line 2
    ...
    Etc.

    I hope this helps you out.
    -Excalibur

  7. #7

    Thread Starter
    Lively Member Scorpionz's Avatar
    Join Date
    Feb 1999
    Location
    lafayette/kaplan, louisiana, usa
    Posts
    72
    NOTE: i'm working with vb5 which does not support split

  8. #8
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Try this:
    Code:
    Private Sub Command1_Click()
        Dim i As Integer
        Dim itmList As ListItem
        
        On Error Resume Next
        
        With ListView1
            'add column headers
            .ColumnHeaders.Add Text:="Column1"
            .ColumnHeaders.Add Text:="Column2"
        
            For i = 0 To List1.ListCount - 1 Step 2
                Set itmList = .ListItems.Add(, , List1.List(i))
                itmList.SubItems(1) = List1.List(i + 1)
            Next
        End With
    End Sub
    Of course this is hard coded, since it's hard to predict what text you going to look for every time.

  9. #9

    Thread Starter
    Lively Member Scorpionz's Avatar
    Join Date
    Feb 1999
    Location
    lafayette/kaplan, louisiana, usa
    Posts
    72
    cool, thanks serge... but is there a way to do it without the use of a listbox?

  10. #10
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    If you are loading a text file in during runtime, use Line Input. Keep a counter going (X = X + 1), look at X instead.

    Code:
    Dim FF As Integer
    
    FF = FreeFile()
    Open TEXT-FILE For Input As #FF
        Do While NOT(EOF(FF))
            Line Input #FF, tmpLine
            X = X + 1
            If Int(X / 2) = X / 2 Then
                'Put your code here for ColumnB
                Else
                'Put your code here for ColumnA
            End If
        Loop
    Close #FF
    You could set up your own split function, or get one from Planet-Source-Code.Com, or check the tips section here. There is a user created VB5 version of Split somewhere ...

    This Is a Quick & Dirty Split Method for VB5 that I thew together. I tested it to work, but we'll have to see

    Code:
    Dim splitArray() As String
    
    Private Sub Command1_Click()
        Split tmpLine, vbCrLf
        For x = LBound(splitArray) To UBound(splitArray)
            MsgBox splitArray(x)
        Next x
    End Sub
    
    Private Sub Split(strSplit As String, strDelim As String)
        Dim splStart As Integer
        Dim splEnd As Integer
        Dim splCount As Integer
    
        splCount = -1
        splStart = 1
        
        Do While InStr(splStart, strSplit, strDelim) > 0
             splCount = splCount + 1
             splEnd = InStr(splStart, strSplit, strDelim)
             ReDim Preserve splitArray(splCount)
             splitArray(splCount) = Mid(strSplit, splStart, splEnd - splStart)
             splStart = splEnd + Len(strDelim)
        Loop
        If Len(strSplit) > splEnd Then
            splCount = splCount + 1
            ReDim Preserve splitArray(splCount)
            splitArray(splCount) = Mid(strSplit, splStart)
        End If
        If splitArray(splCount) = "" Then ReDim Preserve splitArray(splCount - 1)
    End Sub
    [Edited by ExcalibursZone on 11-02-2000 at 04:40 PM]
    -Excalibur

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