?
Solved

Splitting a Stack, Inserting into a ListView

Posted on 2003-03-04
50
Medium Priority
?
205 Views
Last Modified: 2010-05-01
High points b/c I need it answered quickly and throughly.

Anywyas..

I have a variable called listStackUser

listStackUser looks a lil like this

  username,0|username1,0|username2,2|username3,2|  

Username = the persons alias
the number = the image on the imagelist ot put next to their name

I need for the code to do this..

take listStackUser

split it using |
with leftover username,0
or whatever.

and then split it again so its
username (variable = username)
and
0 (variable = adminnumber)

and then add that to the userlist like this
lstUsersClient.ListItems.Add , , username, , adminnumber


And loop it or whateve ryou have to do, so it adds all the users from the stack to the lstUsersClients.
Now, the only thing is that I want them sorted alphabetically, in this order, dev, admin, peon

Dev's admin number is 0
Admins is 1
Peons is 2

Hopefully you understand what I need.
Thank you.
0
Comment
Question by:Zot
[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
  • 15
  • 15
  • 11
  • +2
50 Comments
 

Accepted Solution

by:
tWiZtEr_RX earned 600 total points
ID: 8069073
Dim UserNameArray() As String
Dim UserPictureArray() As String
Dim SeperationArray() As String

Private Sub cmdParse_Click()
On Error Resume Next
Dim i As Integer
SeperationArray = Split(txtParseMe, "|", -1)
For i = 0 To UBound(SeperationArray) - 1
List1.AddItem SeperationArray(i)
Next
List1.AddItem "Done with SeperationArray"
For i = 0 To UBound(SeperationArray) - 1
ReDim Preserve UserNameArray(0 To UBound(SeperationArray))
UserNameArray(i) = Left(SeperationArray(i), (InStr(1, SeperationArray(i), ",", vbTextCompare)) - 1)
List1.AddItem UserNameArray(i)
Next
List1.AddItem "Done With UserNames"
For i = 0 To UBound(SeperationArray) - 1
ReDim Preserve UserPictureArray(0 To UBound(SeperationArray))
UserPictureArray(i) = Mid(SeperationArray(i), Len(UserNameArray(i)) + 2, Len(SeperationArray(i)))
List1.AddItem UserPictureArray(i)
Next
End Sub



This took me about 20 minutes to make lol please gimme points, i need them. If u have questions, comment.
0
 

Expert Comment

by:tWiZtEr_RX
ID: 8069111
sorry i didnt read the sorting part.
Are you asking to sort on the UserName's Image number?
thats not hard, but if u want to sort with usernames:
Dev
Admin
Peon
firstly, then anything goes secondly, that would be harder. but im just not sure what you want.
0
 

Author Comment

by:Zot
ID: 8069228
I need to sort from image numbers. sorry about that. 0s come first, then 1s, then 2s. Thx.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Zot
ID: 8069237
I also need it to send the images and the names to the listview.


it should be
lstUsersClient.ListItems.Add , , username, , adminnumber
0
 

Expert Comment

by:tWiZtEr_RX
ID: 8069248
yup
0
 

Author Comment

by:Zot
ID: 8069260
I also need it to send the images and the names to the listview.


it should be
lstUsersClient.ListItems.Add , , username, , adminnumber
0
 
LVL 11

Assisted Solution

by:supunr
supunr earned 600 total points
ID: 8069265
OK, here is the code just to split it....

Private Sub SplitAndAdd(listStackUser As String)
    Dim strCode As String
    Dim UserName As String
    Dim AdminNumber As String
    Dim i As Long
   
    i = 0
    Do While (True) ' inifinite loop until end of list
        On Error Resume Next
        Err.Number = 0
        strCode = Split(listStackUser, "|")(i)
        If (Err.Number <> 0) Then
            If (Trim(strCode) <> "") Then
                UserName = Split(strCode, ",")(0)
                AdminNumber = Split(strCode, ",")(1)
                lstUsersClient.ListItems.Add , , UserName, , AdminNumber
            End If
        End If
    Loop
End Sub


In your question you want it to be sorted.  but the sorting order you described is not clear.  How does Dev come before admin? Are you sorting by the AdminNumber.  I will update the code to do this sorting as well in a min.  This could get you started.  Good Luck!
0
 

Author Comment

by:Zot
ID: 8069266
I also need it to send the images and the names to the listview.


it should be
lstUsersClient.ListItems.Add , , username, , adminnumber
0
 
LVL 11

Expert Comment

by:supunr
ID: 8069277
Ops...forgot to add line i = i+1 inside the do loop.

also change add

If (Err.Number <> 0) then
   Exit do
else
           If (Trim(strCode) <> "") Then
               UserName = Split(strCode, ",")(0)
               AdminNumber = Split(strCode, ",")(1)
               lstUsersClient.ListItems.Add , , UserName, , AdminNumber
           End If
End if

i= i+1
0
 

Expert Comment

by:tWiZtEr_RX
ID: 8069280
I redid the code for ListView
:::
Dim UserNameArray() As String
Dim UserPictureArray() As String
Dim SeperationArray() As String
Private Sub cmdParse_Click()
On Error Resume Next
Dim i As Integer
SeperationArray = Split(txtParseMe, "|", -1)
For i = 0 To UBound(SeperationArray) - 1
ReDim Preserve UserNameArray(0 To UBound(SeperationArray))
UserNameArray(i) = Left(SeperationArray(i), (InStr(1, SeperationArray(i), ",", vbTextCompare)) - 1)
Next
For i = 0 To UBound(SeperationArray) - 1
ReDim Preserve UserPictureArray(0 To UBound(SeperationArray))
UserPictureArray(i) = Mid(SeperationArray(i), Len(UserNameArray(i)) + 2, Len(SeperationArray(i)))
lstUsersClient.ListItems.Add , , UserNameArray(i), , UserPictureArray(i)
Next
End Sub
0
 
LVL 11

Expert Comment

by:supunr
ID: 8069295
OK, here is the full thing....with sorting of the user name

Private Sub SplitAndAdd(listStackUser As String)
    Dim strCode As String
    Dim Users() As UserDesc
    Dim TmpUser As UserDesc
    Dim UsersCount As Long
    Dim i As Long
    Dim j As Long
   
    i = 0
    UsersCount = 0
    Do While (True) ' inifinite loop until end of list
        On Error Resume Next
        Err.Number = 0
        strCode = Split(listStackUser, "|")(i)
        If (Err.Number = 0) Then
            If (Trim(strCode) <> "") Then
                ReDim Preserve Users(UsersCount + 1)
                Users(UsersCount).UserName = Split(strCode, ",")(0)
                Users(UsersCount).AdminNumber = Split(strCode, ",")(1)
                UsersCount = UsersCount + 1
            End If
        Else
            Exit Do
        End If
        i = i + 1
    Loop
   
    ' sort the array
    For i = 0 To UsersCount - 2
        For j = i + 1 To UsersCount - 1
            If (Users(j).UserName < Users(i).UserName) Then
                ' swap the 2 users
                TmpUser = Users(j).UserName
                Users(j).UserName = Users(i).UserName
                Users(i).UserName = TmpUser
            End If
        Next j
    Next i
   
    ' now add items to the list
    For i = 0 To UsersCount - 1
        lstUsersClient.ListItems.Add , , Users(i).UserName, , Users(i).AdminNumber
    Next i
End Sub


if you want to sort by AdminNumber, then simply change the line:
            If (Users(j).UserName < Users(i).UserName) Then
to
            If (Users(j).AdminNumber < Users(i).AdminNumber) Then
0
 

Expert Comment

by:tWiZtEr_RX
ID: 8069299
he doesnt want sorting of username
he wants sorting of the picture's, therefore a bubble sort. im working on it right now.
0
 
LVL 11

Expert Comment

by:supunr
ID: 8069301
You might not have to do the sorting beforehand.  Listview control might already have sort option.  Just investigate about that.  Good Luck!
0
 

Author Comment

by:Zot
ID: 8069303
Developer comes before Admins.
0
 

Expert Comment

by:tWiZtEr_RX
ID: 8069311
Zot, u want 2 columns in the listview, 1 for Username 1 for Picture, then in the picture one, sort by descending
i think.. maybe ascending owell, try them.
0
 
LVL 11

Expert Comment

by:supunr
ID: 8069318
OK, here is the updated code with imagenumber.
Private Type UserDesc
    UserName As String
    AdminNumber As String
    ImageNumber As Long
End Type

Private Sub SplitAndAdd(listStackUser As String)
    Dim strCode As String
    Dim Users() As UserDesc
    Dim TmpUser As UserDesc
    Dim UsersCount As Long
    Dim i As Long
    Dim j As Long
   
    i = 0
    UsersCount = 0
    Do While (True) ' inifinite loop until end of list
        On Error Resume Next
        Err.Number = 0
        strCode = Split(listStackUser, "|")(i)
        If (Err.Number = 0) Then
            If (Trim(strCode) <> "") Then
                ReDim Preserve Users(UsersCount + 1)
                Users(UsersCount).UserName = Split(strCode, ",")(0)
                Users(UsersCount).ImageNumber = Val(Right(Users(UsersCount).UserName, 1))
                Users(UsersCount).UserName = Left(Users(UsersCount).UserName, Len(Users(UsersCount).UserName) - 1)
                Users(UsersCount).AdminNumber = Split(strCode, ",")(1)
                UsersCount = UsersCount + 1
            End If
        Else
            Exit Do
        End If
        i = i + 1
    Loop
   
    ' sort the array
    For i = 0 To UsersCount - 2
        For j = i + 1 To UsersCount - 1
            If (Users(j).ImageNumber < Users(i).ImageNumber) Then
                ' swap the 2 users
                TmpUser = Users(j).UserName
                Users(j).UserName = Users(i).UserName
                Users(i).UserName = TmpUser
            End If
        Next j
    Next i
   
    ' now add items to the list
    For i = 0 To UsersCount - 1
        lstUsersClient.ListItems.Add , , Users(i).UserName, , Users(i).AdminNumber
    Next i
End Sub


Good Luck!
0
 

Author Comment

by:Zot
ID: 8069320
Dim Users() As UserDesc

User-defined type not defined
0
 
LVL 11

Expert Comment

by:supunr
ID: 8069322
well, my mistake again....correct the sorting to ....

    ' sort the array
    For i = 0 To UsersCount - 2
        For j = i + 1 To UsersCount - 1
            If (Users(j).ImageNumber < Users(i).ImageNumber) Then
                ' swap the 2 users
                TmpUser = Users(j)
                Users(j) = Users(i)
                Users(i) = TmpUser
            End If
        Next j
    Next i
0
 
LVL 11

Expert Comment

by:supunr
ID: 8069324
well, my mistake again....correct the sorting to ....

    ' sort the array
    For i = 0 To UsersCount - 2
        For j = i + 1 To UsersCount - 1
            If (Users(j).ImageNumber < Users(i).ImageNumber) Then
                ' swap the 2 users
                TmpUser = Users(j)
                Users(j) = Users(i)
                Users(i) = TmpUser
            End If
        Next j
    Next i
0
 

Author Comment

by:Zot
ID: 8069326
Type mismatch
TmpUser = Users(j).UserName

with
TmpUser =
highlighted
0
 
LVL 11

Expert Comment

by:supunr
ID: 8069329
well, my mistake again....correct the sorting to ....

    ' sort the array
    For i = 0 To UsersCount - 2
        For j = i + 1 To UsersCount - 1
            If (Users(j).ImageNumber < Users(i).ImageNumber) Then
                ' swap the 2 users
                TmpUser = Users(j)
                Users(j) = Users(i)
                Users(i) = TmpUser
            End If
        Next j
    Next i
0
 
LVL 11

Expert Comment

by:supunr
ID: 8069334
well, my mistake again....correct the sorting to ....

    ' sort the array
    For i = 0 To UsersCount - 2
        For j = i + 1 To UsersCount - 1
            If (Users(j).ImageNumber < Users(i).ImageNumber) Then
                ' swap the 2 users
                TmpUser = Users(j)
                Users(j) = Users(i)
                Users(i) = TmpUser
            End If
        Next j
    Next i
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8069337
Option Explicit

Private Type USER_NAME_AND_PIC
   UserName As String
   PicIndex As Integer
End Type

Dim UserNameArray() As USER_NAME_AND_PIC

Private Sub cmdParse_Click()
   CreateUserNameArray
   AddToList
End Sub

Private Sub CreateUserNameArray()
Dim tmpArray() As String
Dim i As Integer

   tmpArray = Split(txtParseMe, "|", -1)
   ReDim Preserve UserNameArray(0 To UBound(tmpArray))
   For i = 0 To UBound(tmpArray) - 1
      With UserNameArray(i)
         .UserName = Left(tmpArray(i), (InStr(1, tmpArray(i), ",", vbTextCompare)) - 1)
         .PicIndex = Mid(tmpArray(i), Len(UserNameArray(i).UserName) + 2, Len(tmpArray(i)))
      End With
   Next i
End Sub

Private Function AddToList()
Dim i As Integer
Dim intIndex As Integer
Dim HighIndex As Integer

   For i = 0 To UBound(UserNameArray) - 1
      With UserNameArray(i)
         If .PicIndex > HighIndex Then HighIndex = .PicIndex
      End With
   Next i
   
   For intIndex = 0 To HighIndex
      For i = 0 To UBound(UserNameArray) - 1
         With UserNameArray(i)
            If .PicIndex = intIndex Then
               lstUsersClient.ListItems.Add , , .UserName, .PicIndex, .PicIndex
            End If
         End With
      Next i
   Next intIndex
End Function

0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8069344
Replace txParseMe with listStackUser
0
 

Author Comment

by:Zot
ID: 8069346
No errors now. But still no names, and no icons!
0
 

Expert Comment

by:tWiZtEr_RX
ID: 8069348
Is the listview even setup properly?
0
 

Expert Comment

by:tWiZtEr_RX
ID: 8069350
with an imagelist and indexes and stuff?
0
 
LVL 11

Expert Comment

by:supunr
ID: 8069354
well, my mistake again....correct the sorting to ....

    ' sort the array
    For i = 0 To UsersCount - 2
        For j = i + 1 To UsersCount - 1
            If (Users(j).ImageNumber < Users(i).ImageNumber) Then
                ' swap the 2 users
                TmpUser = Users(j)
                Users(j) = Users(i)
                Users(i) = TmpUser
            End If
        Next j
    Next i
0
 

Author Comment

by:Zot
ID: 8069377
Type mismatch
       

.PicIndex = Mid(tmpArray(i), Len(UserNameArray(i).UserName) + 2, Len(tmpArray(i)))
0
 

Expert Comment

by:tWiZtEr_RX
ID: 8069385
Zot, do u have an image list all setup, or another way to put images into your ListView?
0
 
LVL 11

Expert Comment

by:supunr
ID: 8069390
well, my mistake again....correct the sorting to ....

    ' sort the array
    For i = 0 To UsersCount - 2
        For j = i + 1 To UsersCount - 1
            If (Users(j).ImageNumber < Users(i).ImageNumber) Then
                ' swap the 2 users
                TmpUser = Users(j)
                Users(j) = Users(i)
                Users(i) = TmpUser
            End If
        Next j
    Next i
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8069392
Zot,

I have tested the code aboce and it works

Try it
0
 

Expert Comment

by:tWiZtEr_RX
ID: 8069399
you need to have already an imagelist right John?
0
 

Author Comment

by:Zot
ID: 8069410
Type mismatch
       

.PicIndex = Mid(tmpArray(i), Len(UserNameArray(i).UserName) + 2, Len(tmpArray(i)))
0
 
LVL 11

Expert Comment

by:supunr
ID: 8069416
well, my mistake again....correct the sorting to ....

    ' sort the array
    For i = 0 To UsersCount - 2
        For j = i + 1 To UsersCount - 1
            If (Users(j).ImageNumber < Users(i).ImageNumber) Then
                ' swap the 2 users
                TmpUser = Users(j)
                Users(j) = Users(i)
                Users(i) = TmpUser
            End If
        Next j
    Next i
0
 

Expert Comment

by:tWiZtEr_RX
ID: 8069424
John, i dont know much about ListView's.
What settings must it have so i can use like List1.Additem

when i use lstUsersClient.ListItems.Add i + 1, "abc", UserNameArray(i)

it only adds the first one and the rest it ignores!
0
 

Author Comment

by:Zot
ID: 8069427
Type mismatch
       

.PicIndex = Mid(tmpArray(i), Len(UserNameArray(i).UserName) + 2, Len(tmpArray(i)))
0
 
LVL 11

Expert Comment

by:supunr
ID: 8069441
well, my mistake again....correct the sorting to ....

    ' sort the array
    For i = 0 To UsersCount - 2
        For j = i + 1 To UsersCount - 1
            If (Users(j).ImageNumber < Users(i).ImageNumber) Then
                ' swap the 2 users
                TmpUser = Users(j)
                Users(j) = Users(i)
                Users(i) = TmpUser
            End If
        Next j
    Next i
0
 
LVL 11

Expert Comment

by:supunr
ID: 8069460
I am sorry about the multiple postings...I been refreshing my window to see if there are any updates and just now found out that it was resending my previous posting.....
0
 

Expert Comment

by:tWiZtEr_RX
ID: 8069465
hahahah and when it gave u that window, u pressed Retry?
0
 
LVL 11

Expert Comment

by:supunr
ID: 8069488
yeap, at least I know now not to do that.  I learn a very good leason from that.  I am new to this website and do not know that.  It is only my 2nd day today.  Anyway, hope you have solved your problem.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8069493
Are you sure you are using ListView and List
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8069503
It is found in common controls 6
0
 

Expert Comment

by:tWiZtEr_RX
ID: 8069507
John i finally got ListView working after finding out index meant which column index.
Now i have 1 Column for Username and 1 column for Picture ID.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8069529
Description of use for previous posting.

Create new exe
Goto projects -> components -> Microsoft common controls 6
Add a textbox (txtParseMe) that I added your string to
A Command button cmdParse
An ImageList with three pictures added to it
Add a listView lstUsersClient

With lstUsersClient click right mouse and properties
Goto the image list tab and pick imagelist1 from the top two combos.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8069534
Is it working as you hoped
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8069537
When I ran the test I did not use the column propertys and got a ListView similar to the one that appears when you click File - > New in MsWord
0
 

Expert Comment

by:tWiZtEr_RX
ID: 8069558
damn listview isnt showing the images from the imagelist but its making the 32x32 space thats how big the images are...
its annoying how it doesnt ever work for me for like 5 hours
0
 

Expert Comment

by:tWiZtEr_RX
ID: 8069564
Opps, i didnt goto View and select SmallIcons or anything else. i got it working now
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9046891
Hi Zot,
It appears that you have forgotten to close this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Split points between: tWiZtEr_RX and supunr

Zot, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept THIS comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 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