Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2002-05-25
11
Medium Priority
?
462 Views
Last Modified: 2010-05-02
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
----------------------------------------------------------
0
Comment
Question by:investfx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7034574
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
0
 

Author Comment

by:investfx
ID: 7034648
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?

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7034666
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:investfx
ID: 7034667
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?

0
 

Author Comment

by:investfx
ID: 7034672
Hi again Anthony,

Unfortunatly that did not help, the output is still the same, on one line. I am using VB6 also...
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7034707
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
0
 

Author Comment

by:investfx
ID: 7035076
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
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 400 total points
ID: 7035267
I think the point we are both missing is that the array Vnt only contains one element.  If you debug your code you will see that it only loops through once.  Now what you have to do is determine if is is a CR, LF or CRLF separating the elements.  I would venture to guess that it is a CRLF, so the following should work:

'ReDim strParse(UBound(iVnt) + 1)
strParse = Split(iVnt, vbCrLf)
For i = LBound(iVnt) To UBound(iVnt)
' If iVnt(i) <> "" Then strParse(i) = iVnt(i)
  Debug.Print strParse(i)
  LstChkunits.AddItem strParse(i)
Next

You may also want to post the result of this function app.Getxxxx(Chr(9)).  I believe there is a simpler way of skinning this cat.

Anthony
0
 

Author Comment

by:investfx
ID: 7037656
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
0
 

Author Comment

by:investfx
ID: 7037658
Points for Anthony
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7037859
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
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question