Im trying to populate Listbox with Columns but I didn't get success. Below is my code. I populated Listbox with text file:

on users.txt:

1|Jhon|street 3|12/03/1985 
2|Peter|strt2|29/09/2009
3|Scoby|street 1|11/02/2001

Here is my code to populate , as you can see there's delimiter "|".

Public Sub Pathway(way As Variant) 'SET DATABASE FOLDER
     way = App.Path & "\data"
End Sub

Private Sub PopulateListbox()
  Call Pathway(way) 'SET FOLDER WAY
  List1.Clear

Dim fNum As Integer
Dim sInput As String
Dim iInd As Integer
'- Holds list item information
Dim strList As String


    'To load the ListBox from file:
    fNum = FreeFile
    'List1.Clear
    Open way & "\users.txt" For Input As #fNum
    Do Until EOF(fNum)
       Input #fNum, sInput
     '-====================
        '- Increment item count
        rCT = rCT + 1
        '- Resize array to match item count
        ReDim Preserve nmInfo(rCT)
        '- Add data to the array
        nmInfo(rCT) = sInput
        '- Use  VB string "Replace" function to remove the pipes
        strList = Replace(nmInfo(rCT), "|", Space(1))
        '- Add to listbox
        List1.AddItem strList 'sInput
     '-======================        
    Loop
    Close #fNum

End Sub

I would like to ask for your help to add (4) Columns to my Listbox. I tried Listbox property but it didn't work.

Thanks in advance :)

Recommended Answers

All 62 Replies

Use a ListView, then add SubItems

For example:

        Set itmx = ListView1.ListItems.Add()
        itmx.Text = FirstColumnText$
        itmx.SubItems(1) = SecondColumnText$
        itmx.SubItems(2) = ThirdColumnText$

etc.

Be sure to change the View to 3 - lvwReport

how would the Loop look with this code?
Could you give me an example of how the source code for the listview would look?

What does the contents of the users.txt file look like? Send a few lines and I'll send you the loop. Also, why are you saving the lines in the nmInfo array?

the file name : App.Path & data\users.txt
Content:

1|Jhon|street 3|12/03/1985 
2|Peter|strt2|29/09/2009
3|Scoby|street 1|11/02/2001
...
...
...

Use a ListView control and set it's View to lvwReport. Then in the Properties, add the number of columns - in this example 4. Add column headers if desired.

    ListView1.ListItems.Clear
    fNum = FreeFile
    Open "users.txt" For Input As #fNum
    Do Until EOF(fNum)
        Input #fNum, Tmp$
        Result = Split(Tmp$, "|")
        Set itmx = ListView1.ListItems.Add()
        itmx.Text = Result(0)
        itmx.SubItems(1) = Result(1)
        itmx.SubItems(2) = Result(2)
        itmx.SubItems(3) = Result(3)
    Loop

This will result in the screenshot attached.

Best wishes.

Example.jpg

Hey SCBWV,

can you upload your project please? or can you send me it please? got some errors when I tried.

Attached.

Hey SCBWV,
can you have a look my project please, I got error:

Tmp$
Compile error:
Variable not defined

I open your project its works fine, but in my project I got this error.

hi SCBWV,
I changed my lisbox for Listview . My routines are working except Delete and Search , I got some errors. Can you help me with routines Delete and Search ?

Private Sub Command4_Click() 'SEARCH BUTTON

    Dim i As Long, Lindex As Long

    If IsArray(a) Then
        Me.ListView1.ListItems = a
    Else
        Me.ListView1.ListItems.Clear
    End If
    Lindex = Me.Combo1.ListIndex + 1
    If Lindex = 0 Then Exit Sub
    With Me.ListView1
        If .ListItems Then
            For i = .ListItems - 1 To 0 Step -1
               If Val(Split(.List(i, 3), "/")(1)) <> Lindex Then .RemoveItem i
            Next
        End If
    End With
End Sub



Private Sub DeleteData() ' DELETE DATA

  Call Pathway(way) 'SET FOLDER WAY
    Dim i As Long, ii As Long, n As Long, txt As String
    If Me.ListView1.ListIndex = -1 Then Exit Sub
    X(Me.ListView1.ListIndex) = Chr(1)
    X = Filter(X, Chr(2), 0)
    If UBound(X) > -1 Then
        txt = Join(X, vbNewLine)
    Else
        X = Empty
    End If
    Open (way & "\users.txt") For Output As #1
        Print #1, txt;
    Close #1

    Call PopulateListview
End Sub

See my project attached.

I'd be glad to, but I don't use .rar files so I have no way of opening your attachment.

sorry , my bad.

Fixed. I added several comments to the code to help explain. Also added a subroutine for the Combo1_Click event.

Best wishes.

Hi SCBWV,
I found a bug in DeleteData routine:
it was:

Private Sub DeleteData() ' DELETE DATA
    Call Pathway(way) 'SET FOLDER WAY

    'If there's no selected item, exit sub
    If ListView1.SelectedItem = -1 Then Exit Sub
    'Remove the selected item
    ListView1.ListItems.Remove (ListView1.SelectedItem.Index)
    f = FreeFile
    Open (way & "\users.txt") For Output As #f
        'Rebuild the users.txt file from the lines left in the ListView
        For j = 1 To ListView1.ListItems.Count
            Print #f, ListView1.ListItems(j) & "|";
            Print #f, ListView1.ListItems(j).SubItems(1) & "|";
            Print #f, ListView1.ListItems(j).SubItems(2) & "|";
            Print #f, ListView1.ListItems(j).SubItems(3) & "|"
        Next
    Close #f
End Sub

the last line (Print #f, ListView1.ListItems(j).SubItems(3) & "|") was adding : "|" and if you try to add new Data you gonna get error . then I removed the last delimiter and fixed. but there's another bug:
After delete data we need delete the 'ENTER' as well otherwise will get error in PopulateListView :
for example:
1|Jhon|street 3|12/03/1985

<- we need delete this space as well 

3|Scoby|street 1|11/02/2001

And another thing , the Search is not working for me :/ 'the colors not change
which version of Microsoft Visual Basic are you developing this project?

see in attached

see attached

the Search button is searching for the day and not the month as it should.

Sorry. I didn't notice the dates were Day/Month/Year and not Month/Day/Year.

Fixed.

Super! Searching for months successful, now just need figure out how to remove that 'ENTER' I mean the blank space when delete data.
It happen when delete data in txt file and try to add new data in txt file.

Try this. I made significant changes. If you have any questions about the code, let me know.

very good , but the project didn't create the folder itself, my method before check if the folder exist then create new folder if not skip for next step. also you forgot the counter. but I added mine previous code and it worked well. I saw you implemented : Microsoft Scripting Run-time. you're master :) thanks for help me.

I did some changes , see attached.

My thoughts and changes.

Well done! I noticed that we didn't declare : Option Explicit at top of form code, If we declare we will get errors. do you know why ?

Option Explicit forces you to declare all variables before they are used with Dim, Private, Public, ReDim, or Static statements. All your variables were declared except:

Dim Tmp As String
Dim Result() As String

in PopulateListview. Using Option Explicit is a good habit as it can help avoid incorrectly typing the name of an existing variable, which would be apparent when it raises an error.

I got it. See new changes. it works , but I just would like to ask you do you know how to separate this project in Columns like this link?:
link:
https://stackoverflow.com/questions/38796647/how-to-load-data-into-a-multi-column-listbox-vba-vb6

see attached

I'm not sure what you're trying to achieve by using a ListBox over a ListView. Columns in a ListBox is overly complicated to implement in code and has the disadvantage of columns not aligning when text is longer than the space allotted for the column. Additionally, columns in a ListBox does not allow the user to adjust the column width to their desired size. The ListBox does have a column property, but that only changes the scrolling behavior from vertical to horizontal. An example is provided in the Help (F1) for the Column property.

Perhaps I can provide a better answer if I knew why you want to use a ListBox instead of a ListView.

I tested it on another laptop with Listview and had to download the .ocx file and install it to run this simple app. With Listbox I didn't need to download any files, I just extracted the file and it opened normally. I wouldn't mind having columns at a fixed size, so if they lined up for the look that would be nice.

When you install your app on another system, the .ocx will be installed along with your app.

You can simulate columns in a ListBox by including a tab in the text string

ListBox1.AddItem "Column 1" & vbTab & "Column 2"

Again, with variable length strings, it may not align perfectly, so you may have to add additional tabs based on the length of the strings.

I did what you said and it turned out really weird. hahahaha
if there was a way to fix size for all the columns it would be good.
I did in Excel VBA look attached. but the properties I used in Listbox don't work in vb6, I dont know why.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.