Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

Splitting a Stack, Inserting into a ListView

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
Zot
Asked:
Zot
  • 15
  • 15
  • 11
  • +2
2 Solutions
 
tWiZtEr_RXCommented:
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
 
tWiZtEr_RXCommented:
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
 
ZotAuthor Commented:
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!

 
ZotAuthor Commented:
I also need it to send the images and the names to the listview.


it should be
lstUsersClient.ListItems.Add , , username, , adminnumber
0
 
tWiZtEr_RXCommented:
yup
0
 
ZotAuthor Commented:
I also need it to send the images and the names to the listview.


it should be
lstUsersClient.ListItems.Add , , username, , adminnumber
0
 
supunrCommented:
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
 
ZotAuthor Commented:
I also need it to send the images and the names to the listview.


it should be
lstUsersClient.ListItems.Add , , username, , adminnumber
0
 
supunrCommented:
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
 
tWiZtEr_RXCommented:
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
 
supunrCommented:
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
 
tWiZtEr_RXCommented:
he doesnt want sorting of username
he wants sorting of the picture's, therefore a bubble sort. im working on it right now.
0
 
supunrCommented:
You might not have to do the sorting beforehand.  Listview control might already have sort option.  Just investigate about that.  Good Luck!
0
 
ZotAuthor Commented:
Developer comes before Admins.
0
 
tWiZtEr_RXCommented:
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
 
supunrCommented:
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
 
ZotAuthor Commented:
Dim Users() As UserDesc

User-defined type not defined
0
 
supunrCommented:
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
 
supunrCommented:
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
 
ZotAuthor Commented:
Type mismatch
TmpUser = Users(j).UserName

with
TmpUser =
highlighted
0
 
supunrCommented:
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
 
supunrCommented:
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
 
JohnMcCannCommented:
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
 
JohnMcCannCommented:
Replace txParseMe with listStackUser
0
 
ZotAuthor Commented:
No errors now. But still no names, and no icons!
0
 
tWiZtEr_RXCommented:
Is the listview even setup properly?
0
 
tWiZtEr_RXCommented:
with an imagelist and indexes and stuff?
0
 
supunrCommented:
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
 
ZotAuthor Commented:
Type mismatch
       

.PicIndex = Mid(tmpArray(i), Len(UserNameArray(i).UserName) + 2, Len(tmpArray(i)))
0
 
tWiZtEr_RXCommented:
Zot, do u have an image list all setup, or another way to put images into your ListView?
0
 
supunrCommented:
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
 
JohnMcCannCommented:
Zot,

I have tested the code aboce and it works

Try it
0
 
tWiZtEr_RXCommented:
you need to have already an imagelist right John?
0
 
ZotAuthor Commented:
Type mismatch
       

.PicIndex = Mid(tmpArray(i), Len(UserNameArray(i).UserName) + 2, Len(tmpArray(i)))
0
 
supunrCommented:
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
 
tWiZtEr_RXCommented:
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
 
ZotAuthor Commented:
Type mismatch
       

.PicIndex = Mid(tmpArray(i), Len(UserNameArray(i).UserName) + 2, Len(tmpArray(i)))
0
 
supunrCommented:
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
 
supunrCommented:
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
 
tWiZtEr_RXCommented:
hahahah and when it gave u that window, u pressed Retry?
0
 
supunrCommented:
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
 
JohnMcCannCommented:
Are you sure you are using ListView and List
0
 
JohnMcCannCommented:
It is found in common controls 6
0
 
tWiZtEr_RXCommented:
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
 
JohnMcCannCommented:
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
 
JohnMcCannCommented:
Is it working as you hoped
0
 
JohnMcCannCommented:
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
 
tWiZtEr_RXCommented:
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
 
tWiZtEr_RXCommented:
Opps, i didnt goto View and select SmallIcons or anything else. i got it working now
0
 
DanRollinsCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 15
  • 15
  • 11
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now