Link to home
Start Free TrialLog in
Avatar of investfx
investfx

asked on

Populating array items into a listbox on separate lines in the listbox.

Hi All,

Here is my code:
It recieves csv string variable from a delphi function,
and splits the string into an array of separate items.
Each item is made up of 2 separate integers and a string value. I populate a list box with each item.
Question A:
Each splitted item is not going on separate lines in my listbox, rather all on one line. How can I separate each element in the array into a separate line??
e.g. as follows:
Data1   Data 2 Values
1        445    namesa
2        556    names43
3        778    mentre

I thought that this would work but it does not:
LstChkunits.AddItem (strParse(i) & Chr(10))-- why??

Question B:  If I later wanted to just display values in the list box but have the data1 as a property(hidden) from the user associated with values, how would it look like??

Thanks.

---------------------------------------
Dim iVnt As Variant
Dim strParse() As String
Dim funds As String
Dim i As Integer
Dim j As String
funds = app.Getxxxx(Chr(9))
    If Len(funds) > 0 Then
    iVnt = split(funds, ",")
    ReDim strParse(0)
   
        If Not IsMissing(iVnt) Then
            If VarType(iVnt) = 8200 Or VarType(iVnt) = 8204 Then
            ReDim strParse(UBound(iVnt) + 1)
            For i = LBound(iVnt) To UBound(iVnt)
              If iVnt(i) <> "" Then strParse(i) = iVnt(i)
              Debug.Print strParse(i)
                For j = LBound(iVnt) To UBound(iVnt)
               LstChkunits.AddItem (j)
                 Next
             ' LstChkunits.AddItem (strParse(i) & Chr(10))
             Next
            Else
              If VarType(iVnt) = vbString Then strParse(0) = iVnt(0)
              End If
        End If
        End If
End Sub
----------------------------------------------------------
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

The List Box control interprets the Chr(10) as a any non-printable character, it has no more significance than that. In order for each line to be a separate list item you must individually call AddItem for each one.

In other words:
LstChkunits.AddItem "1        445    namesa"
LstChkunits.AddItem "2        556    names43
LstChkunits.AddItem "3        778    mentre"

I realize I am hardcoding the values, but it is to give you the idea.

Question B)  Take a look at the ItemData property.  This is a list of integers that can be associated to each item.  So you could write:
LstChkunits.AddItem "445    namesa"
LstChkunits.ItemData(LstChkunits.NewIndex) = 1
LstChkunits.AddItem "556    names43
LstChkunits.ItemData(LstChkunits.NewIndex) = 2
LstChkunits.AddItem "778    mentre"
LstChkunits.ItemData(LstChkunits.NewIndex) = 3

This way you can use the values without displaying them.

Anthony
Avatar of investfx
investfx

ASKER

Hi Anthony !
Thanks for your reply.
However i still can not get this working. I do not see why the following does not work:
----------------------------------
If Not IsMissing(iVnt) Then
             If VarType(iVnt) = 8200 Or VarType(iVnt) = 8204 Then
              ReDim strParse(UBound(iVnt) + 1)
               For i = LBound(iVnt) To UBound(iVnt) Step -1
                 If iVnt(i) <> "" Then strParse(i) = iVnt(i)
                   Debug.Print strParse(i)
                   LstChkunits.AddItem strParse(i)
               Next
--------------------
as it is doing what you suggest, in a for loop. In debug mode, I can see in the "Immediate" window the correct display but not in the listbox on my form, where the contents of the array are being put on one line.

Any other ideas?

Assuming you are using vb6 (and not vb5) change this line:
LstChkunits.AddItem strParse(i)
to
LstChkunits.AddItem Replace(strParse(i), vbNullChar, " ")

Let me know how it goes.

Anthony
Hi Anthony !
Thanks for your reply.
However i still can not get this working. I do not see why the following does not work:
----------------------------------
If Not IsMissing(iVnt) Then
             If VarType(iVnt) = 8200 Or VarType(iVnt) = 8204 Then
              ReDim strParse(UBound(iVnt) + 1)
               For i = LBound(iVnt) To UBound(iVnt) Step -1
                 If iVnt(i) <> "" Then strParse(i) = iVnt(i)
                   Debug.Print strParse(i)
                   LstChkunits.AddItem strParse(i)
               Next
--------------------
as it is doing what you suggest, in a for loop. In debug mode, I can see in the "Immediate" window the correct display but not in the listbox on my form, where the contents of the array are being put on one line.

Any other ideas?

Hi again Anthony,

Unfortunatly that did not help, the output is still the same, on one line. I am using VB6 also...
Please post the WHOLE code in this procedure as well as the debug output as disaplyed EXACTLY in the immediate window.

Also, explain why you are doing this:
For i = LBound(iVnt) To UBound(iVnt) Step -1

if LBound(iVnt) = 0 and UBound(iVnt)= 3 than guess how many times the i will print:

For i = 0 To 3 Step -1
   Debug.Print i
Next

Answer: 0

Anthony
Sorry to confuse you, "For i = LBound(iVnt) To UBound(iVnt) Step -1" was just a test, I did not have it in my first piece of code. I have taken it out and tested your recommendation again but I still have the same problem.

Here is the full block as requested.
Thsnks for your help.
-------------------------------------
Private Sub populate_chkunits()

Dim iVnt As Variant
Dim strParse() As String
Dim funds As String
Dim i As Integer
Dim j As String
funds = app.Getxxxx(Chr(9))
    If Len(funds) > 0 Then
    iVnt = Split(funds, ",")
    ReDim strParse(0)
   
        If Not IsMissing(iVnt) Then
             If VarType(iVnt) = 8200 Or VarType(iVnt) = 8204 Then
              ReDim strParse(UBound(iVnt) + 1)
               For i = LBound(iVnt) To UBound(iVnt)
                 If iVnt(i) <> "" Then strParse(i) = iVnt(i)
                   Debug.Print strParse(i)
                   LstChkunits.AddItem Replace(strParse(i), vbNullChar, "")
               Next
             Else
                If VarType(iVnt) = vbString Then strParse(0) = iVnt(0)
                End If
             End If
   End If
End Sub

--------------------------------
This is he output in the immediate window:

TestIdent UserIdent    Name

210 801 example values1

211 802 example values2

212 803 example values3
etc...

now all I want is the same output in the listbox. Currently it is just putting everything on one line as follows:

TestIdent UserIdent    Name||210 801 example values1||212 803 example values3
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
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
Hi Anthony,

Yes that was it. Thanks very much for your help, I can now split the string variable into an array separated by "vbcr". This means I get my individual 3 value datas on separate lines.
This is my code:
Dim iVnt As String
Dim strParse() As String
Dim funds As String
Dim i As Integer
funds = MIG21.app.Getxxxx(vbTab)

      strParse = Split(funds, vbCr)
         For i = LBound(strParse) + 1 To UBound(strParse)
           Debug.Print strParse(i)
           CmbFundID.AddItem RTrim(Replace(strParse(i), vbNullChar, ""))
            i = i + 1
         Next
End Sub

However I am still unclear as to how I can manipulate each string i.e.:     1|  445|   namesa|
to show or hide column 1 or column2 etc.
I presume that I would need a sub-loop, but I cannot make this work. ANy further ideas??

Regards,  John
Points for Anthony
If there is only one spacebetween columns, than you can use the Split function to get at individual fields"

Dim fld As Variant
Dim J As Integer

fld = Split(strParse(i), " ")
For J = 0 to UBound(Fld)
   Debug.Print fld(J)
Next

If there is more than one space between columns:
Dim fld As Variant
Dim J As Integer

fld = Split(strParse(i), " ")
For J = 0 to UBound(Fld)
   If Len(fld(J)) Then
      Debug.Print fld(J)
   End If
Next

Anthony