Random assigning caption

  • 13 years ago
    Hi. Im beginner in VB 6.0, i tried to find how to do it, but i wasnt succesful.

    At standard .exe. form, I have defined 9 buttons with captions "1" ,"2" ,"3" ,"4", "5", "6", "7", "8" and "X"

    The problem is, that i want, after each start of programme (or possibly after clicking special button) to
    randomly assing each of this captions (written above) to each button.

    I suppose that it is some script, written in "form.load", or similar, that can do that.


    Thanks for any help.











  • 13 years ago
    Hi,

    The simplest way is to creat an array of command buttons indexed from 0 to 8 and then paste the below code in the code view. First you will have to:
    • Create a button and name it "cmd".
    • Create array of 9 buttons named "cmdch". (to create an array of buttons create 1 button, name it, set index property to 0, copy and paste it 8 times and you will get 9 buttons having the same name with different indexes).

    • Paste the below code:
    Dim Coun, Coun1 As Integer 'COUNTERS
    Dim Cap(9) As String 'ARRAY FOR CAPTIONS
    Dim Temp As String 'VARIABLE TO STORE RANDOM VALUE IN


    Private Sub cmd_Click()

    'EMPTY ANY AVAILABLE CAPTIONS
        For Coun1 = 0 To 8
       
            Me.cmdCh(Coun1).Caption = ""
           
        Next




       
    'FILL THE ARRAY WITH THE CAPTIONS (1...8 , X)
        For Coun = 1 To UBound(Cap) - 1
       
            Cap(Coun) = Coun
       
        Next
        Cap(9) = "X"






       
    '--------------------------
        For Coun1 = 0 To 8
       
    E:


    'GENERATE A RANDOM INDEX FOR THE ARRAY AND GET ITS CORRESPONDING VALUE
            Temp = Cap(Round(Rnd() * 9))
           
            For Coun = 0 To 8


           
    'IF CAPTION IS ALREADY AVAILABLE GENERATE ANOTHER ONE
                If Temp = Me.cmdCh(Coun).Caption Then GoTo E
          
            Next



    'ASSIGN VALUE TO CAPTION
            Me.cmdCh(Coun1).Caption = Temp
       
        Next

    End Sub

    Private Sub Form_Load()

    End Sub









    Hope this work for you.



























Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” - Dennis Ritchie