Results 1 to 9 of 9

Thread: Control Array - .Net alternative?

  1. #1

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Control Array - .Net alternative?

    I was just wondering if there is and alternative for control arrays in .net as these were quite handy in vb6?

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Control Array - .Net alternative?

    You can create your own pseudo control arrays in .Net... the only thing you can't do is create multiple controls with the same name.

    For example you can dimension an array of controls like this :

    Code:
    Dim MyControls(20) as control
    and add to it like this :

    Code:
    For X as integer = 0 to 20
       Dim MyNewControl As New Label
       MyNewControl.Text = "Label " & x.ToString
       MyControls(MyControls.count) = MyNewControl
    Next X

  3. #3

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Re: Control Array - .Net alternative?

    Ok cool thanks I shall give that a try.

    I have another question, maybe you can help. If I was using the following code:

    Code:
    For Each Cntrl as Control in me.controls
    
         'Some Code'
    
    Next Cntrl
    Is there a way of controlling the order of controls it loops through? Does it go based on which ones are created first?

    Im basically helping someone with a game but the way I have gone about it is changing the text of labels one by one. In the game there is 160 labels so not very efficient to change these one by one.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Control Array - .Net alternative?

    It most likely goes through the collection based on the order the controls was added to the collection.
    Altough this question really should be in a separate thread, could I ask in what way you would like to control the iteration?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Control Array - .Net alternative?

    Is there a way of controlling the order of controls it loops through?
    As Atheist says, this should probably be a new thread, but I'll just chuck in a thought : If you are using VB.Net 2008 you can probably use LINQ to get all the controls ordered by the value of a specific property.

  6. #6

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Re: Control Array - .Net alternative?

    Well basically I'm adding data from a text file into the labels text property so i basically need to iterate through the labels in some sort of order such as Label1, Label2, Label3 so I can make sure the right data is going in to each label.

  7. #7
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Control Array - .Net alternative?

    You could add an identifier into the tag property of each control in the collection (either at design time or at run time if you are creating controls on the fly).

    Then you could look up the appropriate value for the control using that identifier.

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Control Array - .Net alternative?

    Quote Originally Posted by mouse88 View Post
    ...
    Is there a way of controlling the order of controls it loops through? Does it go based on which ones are created first?

    Im basically helping someone with a game but the way I have gone about it is changing the text of labels one by one. In the game there is 160 labels so not very efficient to change these one by one.
    The order of controls is meaningless for a FOR EACH... NEXT loop. The iterator usually picks them up in order, but this is not guaranteed.
    If you want to get controls in fixed order, you should use a loop variable.

    vb.net Code:
    1. For i as Integer = 0 To  Me.Controls.Count -1
    2.     MsgBox(Me.Controls(i).Name)
    3. Next
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Re: Control Array - .Net alternative?

    Ok problem solved that works fine. I was originally trying to identify the control by checking if the name contained a number but then when it came to checking say Label11 and the value of I in my integer loop was 1 this would get the wrong data.

    Thanks for the help

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