Results 1 to 21 of 21

Thread: ? l i s t b o x e s ?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32
    ok this is a stupid question but just on of those pain-in-the-ass things you can't do:

    i have a listbox which generates a random amount of random strings, when i close the form the strings are generated again,which i don't want, at the moment i'm kinda doing this

    udtEMPL.Em1 = ListBox. ??????

    then for the next item in the listbox i would have

    udtEMPL.Em2 = Listbox. ?????

    i want this to be done automatically by the prog.
    can anyone help?
    thanks
    CGB

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

    <?>

    Code:
    Generate your strings and store them in an array:
    Dim myArr()
    For i = whatever to whatever
    ReDim Preserver myArr(i)
      myArr(i) = i & (rnd * 100) + 1
    next i
    
    for i = lbound(myArr) to uBound(myArr)
       list1.additem myArr(i)
    next i
    'You could then store your array in a file it wanted
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32
    thanks
    CGB

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32
    but... it doesn't add integers it adds strings, so that doesn't work, here is my existing code:

    Sub GENITEMZ()
    Employeez.Clear
    Empler.Clear
    HMNY = Int((8 * Rnd) + 1)
    Dim Counter As Integer
    Do Until Counter = HMNY
    RAN = Int((8) * Rnd)
    If RAN = 0 Then Employeez.AddItem ("Manual Labourer")
    If RAN = 1 Then Employeez.AddItem ("Manual Labourer")
    If RAN = 2 Then Employeez.AddItem ("Manual Labourer")
    If RAN = 3 Then Employeez.AddItem ("Manual Labourer")
    If RAN = 4 Then Employeez.AddItem ("Bodyguard")
    If RAN = 5 Then Employeez.AddItem ("Bodyguard")
    If RAN = 6 Then Employeez.AddItem ("Scientist")
    If RAN = 7 Then Employeez.AddItem ("Navigator")
    Counter = Counter + 1

    Loop
    End Sub
    CGB

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

    <?>

    what exactly are you looking for as a result of calling
    your function?
    Give me an example of what the correct situtaion would yield.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32
    that code works, the listbox would contain a random number of strings (which works) and when the form is closed and opened again the listbox would contain the same strings in the same order (which doesn't work)
    CGB

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

    <?>

    forgot one little thing
    you need to put this above the Rnd functions then it will work

    Randomize

    Code:
    Option Explicit
    
    
    Sub GENITEMZ()
    Dim ran As Integer
    Dim HMNY As Integer
    
    Employeez.Clear
    Empler.Clear
    Randomize
    HMNY = Int((8 * Rnd) + 1)
    Dim Counter As Integer
    Do Until Counter = HMNY
    Randomize
    ran = Int((8) * Rnd)
    If ran = 0 Then Employeez.AddItem ("Manual Labourer")
    If ran = 1 Then Employeez.AddItem ("Manual Labourer")
    If ran = 2 Then Employeez.AddItem ("Manual Labourer")
    If ran = 3 Then Employeez.AddItem ("Manual Labourer")
    If ran = 4 Then Employeez.AddItem ("Bodyguard")
    If ran = 5 Then Employeez.AddItem ("Bodyguard")
    If ran = 6 Then Employeez.AddItem ("Scientist")
    If ran = 7 Then Employeez.AddItem ("Navigator")
    Counter = Counter + 1
    
    Loop
    End Sub
    
    Private Sub Form_Load()
    Call GENITEMZ
    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

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32
    that works, but when i close the form and then re-open the form the strings in the list box have been generated again.
    CGB

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32
    anyone?
    CGB

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

    <?>

    are you saying you want to generate only once and then
    save the strings on exit for the next time you open?
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32
    yes
    CGB

  12. #12

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32
    anyone?
    CGB

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

    <?>

    what's with this anyone?

    it takes time to write code..don't be so impatient.
    sometimes it takes hours to get answers.

    'this will load your strings once and only once
    'saves them to a file and reloads
    Code:
    Option Explicit
    
    
    Sub GENITEMZ()
    
    Dim ran As Integer
    Dim HMNY As Integer
    
    employeez.Clear
    empler.Clear
    Randomize
    HMNY = Int((8 * Rnd) + 1)
    Dim Counter As Integer
    Do Until Counter = HMNY
    Randomize
    ran = Int((8) * Rnd)
    
        If ran = 0 Then employeez.AddItem ("Manual Labourer")
        If ran = 1 Then employeez.AddItem ("Manual Labourer")
        If ran = 2 Then employeez.AddItem ("Manual Labourer")
        If ran = 3 Then employeez.AddItem ("Manual Labourer")
        If ran = 4 Then employeez.AddItem ("Bodyguard")
        If ran = 5 Then employeez.AddItem ("Bodyguard")
        If ran = 6 Then employeez.AddItem ("Scientist")
        If ran = 7 Then employeez.AddItem ("Navigator")
        Counter = Counter + 1
    Loop
       Call SaveMyListBox
    
    End Sub
    Public Sub SaveMyListBox()
    'Save the contents
    Dim i As Integer
        Open "C:\my documents\myListBox.txt" For Output As #1
            For i = 0 To employeez.ListCount - 1
                Print #1, employeez.List(i)
            Next i
        Close #1
    End Sub
    Public Sub LoadMyListBox()
    'Load the contents
    Dim sListing As String, i As Integer
        Open "C:\my documents\myListBox.txt" For Input As #1
            Do While Not EOF(1)
                Input #1, sListing
                employeez.AddItem sListing
           Loop
        Close #1
    End Sub
    
    Private Sub Form_Load()
    
    Dim sFile$
    
    sFile = "C:\my documents\mylistbox.txt"
    
    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    
    If fs.fileexists(sFile$) = True Then
       Call LoadMyListBox
    Else
        Call GENITEMZ
    End If
        Set fs = Nothing
    
    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

  14. #14

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32
    sorry, just often people will start helping then get fed up and bugger off, thanks anyway.
    is there anyway it could be done without saving it?
    CGB

  15. #15

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32
    i mean when i close that particular form the whole program is not closed because the main form can't be, i just want the listbox contents to be saved while the program is running - you know just have it stored under a public type...
    CGB

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

    <?>


    Code:
    'if you add a public variable (array)
    Option Explicit
    
    Public myArr()
    '*********change loading and unload the file
    
    'Save the contents
    Dim i As Integer
                For i = 0 To employeez.ListCount - 1
                ReDim Preserve myArr(i)
               myArr(i) = employeez.List(i)
            Next i
        Close #1
    End Sub
    
    Public Sub LoadMyListBox()
    'Load the contents
    
    Dim i As Integer
         
         For i = LBound(myArr) To UBound(myArr)
               employeez.AddItem myArr(i)
           Next i
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  17. #17

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32

    Public myArr()
    '*********change loading and unload the file

    'Save the contents
    Dim i As Integer
    For i = 0 To employeez.ListCount - 1
    ReDim Preserve myArr(i)
    myArr(i) = employeez.List(i)
    Next i
    Close #1
    End Sub

    i put this in a module and it says: invalid outside procedure, it doesn't work in the form's code either
    CGB

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

    <?>

    Ok..I will yank your code down and see what is happening.
    Is it the same as in this post or have you changed it.
    If so give me your new stuff.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

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

    <?>

    Code:
    '
    'works..I tested it
    '
    'this goes in a bas module
    Option Explicit
    
    Public myArr()
    
    'This goes in form1
    
    Option Explicit
    
    Sub GENITEMZ()
    
    Dim ran As Integer
    Dim HMNY As Integer
    
    employeez.Clear
    'empler.Clear
    Randomize
    HMNY = Int((8 * Rnd) + 1)
    Dim Counter As Integer
    Do Until Counter = HMNY
    Randomize
    ran = Int((8) * Rnd)
    
        If ran = 0 Then employeez.AddItem ("Manual Labourer")
        If ran = 1 Then employeez.AddItem ("Manual Labourer")
        If ran = 2 Then employeez.AddItem ("Manual Labourer")
        If ran = 3 Then employeez.AddItem ("Manual Labourer")
        If ran = 4 Then employeez.AddItem ("Bodyguard")
        If ran = 5 Then employeez.AddItem ("Bodyguard")
        If ran = 6 Then employeez.AddItem ("Scientist")
        If ran = 7 Then employeez.AddItem ("Navigator")
        Counter = Counter + 1
    Loop
       Call SaveMyListBox
    
    End Sub
    Public Sub SaveMyListBox()
    Dim i As Integer
                For i = 0 To employeez.ListCount - 1
                ReDim Preserve myArr(i)
               myArr(i) = employeez.List(i)
            Next i
    End Sub
    Public Sub LoadMyListBox()
    'Load the contents
    Dim i As Integer
         
         For i = LBound(myArr) To UBound(myArr)
               employeez.AddItem myArr(i)
           Next i
    End Sub
    
    Private Sub Command1_Click()
      Unload me
      Load Form2
      Form2.Show
    End Sub
    
    Private Sub Form_Load()
    
    On Error GoTo QuitNow:
        
        Debug.Print UBound(myArr)
        Call LoadMyListBox
        On Error GoTo 0
        Exit Sub
    QuitNow:
        Call GENITEMZ
    End Sub
    
    'this goes in form2
    
    Option Explicit
    
    Private Sub Command1_Click()
        Unload Me
        Load Form1
        Form1.Show
    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

  20. #20
    Guest
    You can shorten all of those If...Then's with a Choose() function.
    Code:
    employeez.AddItem Choose(Int(Rnd * 8) + 1, "Manual Labourer", "Manual Labourer", "Manual Labourer", "Manual Labourer", "Bodyguard", "Bodyguard", "Scientist", "Navigator")

  21. #21

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32
    this comes a bit late but i have been away during the week: thanks!
    CGB

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