Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By jmcilhinney

Thread: How to create a ControlArray instead of a non-working For-Function!?

  1. #1
    d0minik is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2011
    Location
    Liesing, Wien, Austria, Austria
    Posts
    1
    Reputation
    0

    Question How to create a ControlArray instead of a non-working For-Function!?

    Hi everyone,

    first of all - I hope i posted in the right Forum. My Problem is, that I have some Buttons, Labels and TextBoxes I need to generate after the User told a number of Players. With the Code I'm using now only the last Player will have these Controls working, because they are moved - for example - from Player 1 to Player 3.

    I was looking for several hours in the Internet with Google and so on and found out, that a "ControlArray" should help out, BUT I did not find any help on how to create such a ControlArray. The biggest Problem I have is, that these buttons have to have names like "1", "2", "3" and so on (or if it works somehow to read out the Index of a ControlArray the names will not be necessary).

    This is the code I am using right now to generate the Controls, but as you see it won't work, because the properties will be changed when the For-Function restarts.What I need is a hint on how to get these Controls/Buttons into a Control-Array and then read out the Index-Number to use them in the Functions that are called. Hopefully you know, what I mean.

    Thanks everybody for reading and help!

    EDIT: Got it allready! It's very easy, if you know how to do it :
    Code:
    Public Class Form1
    Private btnTestNumButtons() As System.Windows.Forms.Button
    
        Private Sub CreateButtonArray()
            Dim shtIndex As Short
    
            ReDim btnTestNumButtons(10)
            For shtIndex = 0 To 9
                btnTestNumButtons(shtIndex) = New System.Windows.Forms.Button
                With btnTestNumButtons(shtIndex)
                    .Text() = shtIndex.ToString()    'Set text caption
                    .Size() = New Size(40, 40)     'Set the button size
                    .FlatStyle() = FlatStyle.Popup  'Set the button flat style
                    .BackColor() = System.Drawing.Color.Blue       'Set the background color
                    AddHandler .Click, AddressOf Me.btnTestNumButtons_Click    'Attach handler reference
                End With
            Next
    
            Me.Controls.AddRange(btnTestNumButtons)
            ButtonStackOrder()
    
        End Sub
    This worked with some changes for me!
    Last edited by d0minik; 08-07-2011 at 8:43 AM.

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,504
    Reputation
    1553
    There is no such thing as a ControlArray. That was a VB6 thing. In VB.NET, arrays of controls are exactly the same as any other array. If you want to create an array then just go ahead and create an array. Create the Buttons or whatever other controls you need and assign them to the elements of the array. That's all there is to it. Just like any other array. E.g.
    Dim buttons(2) As Button

    buttons(0) = New Button
    buttons(1) = New Button
    buttons(2) = New Button
    d0minik likes this.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking