Results 1 to 11 of 11

Thread: arrays!

  1. #1
    shuj66
    Guest

    Question

    I have 96 blank labels on my form.
    (label106 to lable201).

    I want to use arrays to generate numbers to be randomly filled in these labels.

    How can I go about doing this.

    Thank you.

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well, you shouldnt have the labels named like that.
    They should be label(106) to label(201).

    To fill with random numbers do :

    Code:
    Dim i As Long
    For i = 0 to 95
        Label(106 + i).Caption = Rnd
    Next i
    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well a faster loop would be this I'd imagine :

    Code:
    Dim i As Long
    For i = 106 to 201
        Label(i).Caption = Rnd
    Next i
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Addicted Member
    Join Date
    Feb 2001
    Location
    Classified
    Posts
    234
    First, i think you will ither have to delete all of your label controls, copy one of them and paste it, answering YES to the popup box, asking about making a control array.

    the other way i think would to rename them all... and make them into an array manualy that way,

    dunno tho.... im probly missing an easyer meathod, but thats the only way ive known

    then you would just go ......



    Const LowNumber = 1
    Const HighNumber = 50

    Private Sub Command1_Click()
    For i = 1 To 96
    Label(i).Caption = Int((HighNumber - LowNumber + 1) * Rnd + LowNumber)
    Next
    End Sub
    My ICQ Status: (85634850)

    Seriously Sick Tshirts

  5. #5
    Addicted Member
    Join Date
    Feb 2001
    Location
    Classified
    Posts
    234
    err... change the line
    For i = 1 To 96
    to
    For i = 0 To 95



    arrays start at 0 :P
    My ICQ Status: (85634850)

    Seriously Sick Tshirts

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    You should convert (via renaming or deleting or whatever) all of the labels, and make a control array.

    But there is another way.
    If you use the scripting control, it allows you to add arbitrary code to your app at runtime.
    So you could actually do something like :

    "Label" & (106 + i) & ".Caption = Rnd"

    Then you'd execute the code at runtime, and it'd work.
    Trust me

    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7
    Guest
    Hi,

    Thanks but how do I get it to print the values on the form in each of the labels.

    The form just has labels on it and I need numbers to be displayed on the labels.
    the labels are named label106 all the way to label206.

    thank you.

  8. #8
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by Ph34R
    err... change the line
    For i = 1 To 96
    to
    For i = 0 To 95



    arrays start at 0 :P
    This one doesnt
    Code:
        Dim var_string(25 To 100) As String
        Dim i As Long
        For i = 25 To 100
            var_string(i) = "osajdoiasjosdiaj"
        Next i
    God I'm really annoying arent I

    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  9. #9
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Shu.
    Delete all of your labels.
    Add a new label.
    Copy and paste it.
    Allow it to use a control array.
    The use our code.

    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  10. #10
    Guest
    plenderj,

    I can't create a control array for my labels because this is just part of my application and I need the labels to be seperate.

    Isn't there any way that I could display blank labels on a form with with random numbers.

    thank you.

  11. #11
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well yeah, you could just simply do this :

    Code:
    Label106.Caption = Rnd
    Label107.Caption = Rnd
    Label108.Caption = Rnd
    Label109.Caption = Rnd
    Label110.Caption = Rnd
    Label111.Caption = Rnd
    Label112.Caption = Rnd
    Label113.Caption = Rnd
    '...
    Label201.Caption = Rnd
    Except a control array would be much slicker.
    Also, as I said you could use the microsoft scripting engine, but thats another story.

    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

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