Results 1 to 8 of 8

Thread: ListBox - urgent please

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    Hi,
    I have a ListBox and two Buttons on my form.
    I need to store the following data for 10 students:

    Name, month of birth, year of birth

    I want to show student's NAMES ONLY in the ListBox sorted:
    - by month they were born
    Or
    - by year they were born
    depending on button pressed.

    I think .ItemData should be used.

    Thanks
    Thanks

    Tomexx.

  2. #2
    Fanatic Member spud's Avatar
    Join Date
    Aug 2000
    Location
    Munster (Germany)
    Posts
    542

    Smile

    Is the ListBox connected to a datadase?

  3. #3
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Gig Harbor, WA; Posts: 89950
    Posts
    360

    Exclamation

    Hmm, is it a DB? Bound or unbound controls?
    If you have a set number of students that will not change, you can set the listbox listindex at design time.
    The listindex will begin with "0" and continue until all the students are listed.
    You can also use the sorted property if you want them alphbeticly or whatever, but without knowing how the students names are stored, (database, random file, sequential file) it is a difficult question to answer.
    Good Luck!
    Lee
    Mahalo
    VB6(SP5), VC++, COBOL, Basic, JAVA
    MBA, MCSD, MCSE, A+
    Computer Forensics

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    No,
    It's not connected to the database.
    It's just a listbox, 2 buttons. One button sorts by month, the other sorts by year. I need to disply student's names in the listbox sorted either by month they were born in or the year depending which button is clicked.

    Data sample
    Name Mo Yr

    "John", 04, 84
    "Ted", 12, 82
    "Steve",07, 80

    If I click on Command1 then the listbox will show names which birth months are from the earliest:
    John
    Steve
    Ted

    By clicking on Command2 the listbox will show names which birth Years are from the earliest:
    Steve
    Ted
    John

    Thanks again
    Thanks

    Tomexx.

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    you could add the names, in their proper order at the start

    i could write some code if you want

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    You should store the itemdata like yymm, for instance 8006 for june 1980 or 9212 for december 1992. No problem with yyyymm either, but no commas.

    Retrieving the year:
    int(list1.Itemdata(index)/100)
    Retrieving the month:
    list1.Itemdata(index) mod 100
    Setting the date
    list1.Itemdata(index) = year *100 + month

    Now i spent some time modifying Sort_shell for this purpose, and i don't have accesss to vb, so there might be bugs, try it!
    [code]
    Sub Sort_shell(Sortmethod as boolean)
    Dim n&, i&, j&, k&, a1$, a2&(1),b1$,b2(1),x%
    x=-Sortmethod
    n = UBound(a)
    k = n \ 2
    While k > 0
    For i = 0 To n - k
    j = i
    a1=list1.list(j):b1=list1.list(j+k)
    a2(0)=int(list1.Itemdata(j)/100):b2(0)=int(list1.Itemdata(j+k)/100)
    a2(1)=list1.Itemdata(j) mod 100:b2(1)=list1.Itemdata(j+k) mod 100
    While (j >= 0) And (a2(x) > b2(x))
    list1.list(j)=a1:list1.list(j+k)=b1
    h=list1.itemdata(j)
    list1.itemdata(j)=list1.itemdata(j+k)
    list1.itemdata(j+k)=h
    If j > k Then
    j = j - k
    Else
    j = 0
    End If
    Wend
    Next i
    k = k \ 2
    Wend
    End Sub

    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Gig Harbor, WA; Posts: 89950
    Posts
    360

    Lightbulb

    Alright, you are putting way too much thought into this.
    Try this!

    Option Explicit

    Private Sub cmdMonths_Click()
    lstNames.Clear
    lstNames.AddItem "Fred"
    lstNames.AddItem "John"
    lstNames.AddItem "Alex"
    ' enter the names in order you want them to be in
    'set the sorted property to false at design time
    End Sub

    Private Sub cmdYears_Click()
    lstNames.Clear
    lstNames.AddItem "Alex"
    lstNames.AddItem "John"
    lstNames.AddItem "Fred"
    ' enter the names in order you want them to be in
    'set the sorted property to false at design time
    End Sub

    Private Sub Form_Load()
    lstNames.Clear 'ensures the listbox is empty as form loads
    End Sub


    ~Hope this helps!
    Lee
    Mahalo
    VB6(SP5), VC++, COBOL, Basic, JAVA
    MBA, MCSD, MCSE, A+
    Computer Forensics

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'this is the long way home but it works
    'your information is stored in a text file (myfile.txt)
    
    '   "John", 04, 84
    '   "Ted", 12, 82
    '   "Steve",07, 80
    '   "Mary",01,75
    '   "Johnny",04,91
    '   "Larry",02,92
    '   "Maggie",02,88
    '   "Annie",04,89
    '   "Angelo",08,86
    '   "Gene",09,86
       
       
    Option Explicit
    '
    
    Private Sub Command1_Click()
    'sort and list by month
    '
    'clear the listbox
    List1.Clear
    '
    Dim sFile As String, intNum As Integer
    sFile = "C:\MyFile.txt"
    intNum = FreeFile
    
    Dim myArrM()  'used to store an assembled version of file
    Dim i As Integer
    Dim var1 As String, var2 As String
    Dim var3 As String, var4 As String
    
    'get the information from the file
    Open sFile For Input As intNum
    'loop to eof
       Do While Not EOF(intNum)
    'redim preserve your array
       ReDim Preserve myArrM(i)
    'read the file information
       Input #intNum, var1, var2, var3
    'rearrange the information as per month/year/name and join
        var4 = var2 & var3 & var1
    'load the new string into your array
        myArrM(i) = var4
    'increment the array
        i = i + 1
       Loop
    'sort the new array
       Call SortStr(myArrM)
    'after sorting the array relist it and extract the name
    
       For intNum = LBound(myArrM) To UBound(myArrM)
          var1 = myArrM(intNum)
          var2 = Len(var1)
          var3 = Right(var1, var2 - 4)  '4 is constant mmyy, the rest is the name
    'display the names in List1
          List1.AddItem var3
        Next
        
    End Sub
    
    
    Sub SortStr(iArray As Variant)
    'sort the array by alpha..the array is a new string mmyyname
         Dim Loop1 As Long
         Dim Loop2 As Long
         Dim lTemp As String
      
         For Loop1 = UBound(iArray) To LBound(iArray) Step -1
           For Loop2 = LBound(iArray) + 1 To Loop1
    
             If iArray(Loop2 - 1) > iArray(Loop2) Then
               lTemp = iArray(Loop2 - 1)
               iArray(Loop2 - 1) = iArray(Loop2)
               iArray(Loop2) = lTemp
             End If
           Next Loop2
         Next Loop1
    End Sub
    
    
    Private Sub Command2_Click()
    'sort and list by year
    '
    'clear the listbox
    List1.Clear
    '
    Dim sFile As String, intNum As Integer
    sFile = "C:\Myfile.txt"
    intNum = FreeFile
    'array to store the new arrangement of file information
    Dim myArrY()
    'variables used in string and file manipulation
    Dim i As Integer
    Dim var1 As String, var2 As String, var3 As String
    Dim myvar4 As String
    'open the file and read it into the array
    Open sFile For Input As intNum
       Do While Not EOF(intNum)
    'redim preserve your array
       ReDim Preserve myArrY(i)
       
       Input #intNum, var1, var2, var3
        myvar4 = var3 & var2 & var1
        myArrY(i) = myvar4
        i = i + 1
       Loop
    'call to sort the new array yymmname
       Call SortStr(myArrY)
    'after sort extract the name and list it to the listbox
       For intNum = LBound(myArrY) To UBound(myArrY)
          var1 = myArrY(intNum)
          var2 = Len(var1)
          var3 = Right(var1, var2 - 4)
          List1.AddItem var3
        Next
        
    End Sub
    
    Private Sub Form_Load()
        List1.Clear
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width