Solved

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

Posted on 2002-05-25
11
439 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
  • 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 100 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now