Results 1 to 6 of 6

Thread: help With a Small prog that is easy to make!!!

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    2

    Post

    Hi I am farely new to Visual Basics and i would like to make a program which generates 6 numbers out of 1-45.

    None of the numbers must appear twice and it should just generate 6 random numbers out of 1-45

    I have made a layout but dont really know any coding so if somone cn help me , pls contact me on
    [email protected]

    or on icq

    44437708
    "Those Who Live By The Sword, get Shot By Those Who Don't"

  2. #2
    Fanatic Member
    Join Date
    Jan 2001
    Location
    Vietnam
    Posts
    613
    Add a command button and a listbox to a form, then copy
    the followin code:

    Private Sub Command1_Click()
    Dim intNumber As Integer

    List1.Clear
    Do While List1.ListCount < 6
    intNumber = GenerateNumber(1, 45)
    If Not IsMember(intNumber, List1) Then
    List1.AddItem intNumber
    End If
    Loop

    End Sub

    Function GenerateNumber(ByVal intLow As Integer, _
    ByVal intHigh As Integer) As Integer
    Randomize
    GenerateNumber = Int((intHigh * Rnd) + intLow)
    End Function

    Function IsMember(ByVal intNumber As Integer, lstBox As ListBox) As Boolean
    Dim intIndex As Integer
    For intIndex = 0 To lstBox.ListCount - 1
    If CStr(intNumber) = lstBox.List(intIndex) Then
    IsMember = True
    End If
    Next intIndex
    End Function

    Hope this helps
    TheBao

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    I have mailed you my project I made, but basically I used the following:
    Code:
    Private Sub Command1_Click()
        Dim BallNo As Integer, found As Boolean, collect As New Collection, i As Integer
           
    On Error Resume Next
        Do Until collect.Count = 6
            BallNo = Int(Rnd * 45) + 1
            collect.Add CStr(BallNo), CStr(BallNo)
            found = (Err = 0)
            If found Then
                Err.Clear
                Do While Not found = True
                    BallNo = Int(Rnd * 49) + 1
                Loop
            Else
                collect.Add BallNo, BallNo
            End If
        Loop
            
        For BallNo = 1 To collect.Count
            Lbl(i).Caption = collect.Item(BallNo)
            i = i + 1
        Next
    End Sub
    Basically, I am using the array and collection objects to store the random numbers, then a loop to check and update the number if it's already taken.

    I then have 6 labels on a form (all with the same name of lbl), the bottom for..next loop selects the label, puts one of the random numbers into it's cation property before selecting the next number, and next label to update.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up

    try to make use of the Microsoft Script Runtime object (Distionary)

    Take a look on my sample code on this Thread

    Cheers!

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    2

    Cool

    Thanx Very Much Guys and thank you too Alex, i will ask u for further help because i am new to VB6 and dont have MSDN...

    Or any VB6 books cuz they cost too much!!!!
    "Those Who Live By The Sword, get Shot By Those Who Don't"

  6. #6
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Too right, I used the learning edition for about 2 years because of this!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

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