• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1472
  • Last Modified:

Using a Variable to Dim a Control

I would like to create a number of controls at run-time. For example, let's say I'm programming a simple game with a board that is 10x10. Visual Basic 2005 does not use control arrays like 6.0 and earlier did. You need to create each control manually or through code. Instead of drawing, editing, and placing 100 objects on the form one by one, I'd like to do this with a For...Next loop if possible.

Let's say I want to name the controls Space00, Space01, Space02... up to Space99. Here's what I have tried so far:

        Dim Ctrl As New Button
        Dim i As Integer

        For i = 0 To 99
            Select Case i
                Case 0 To 9
                    Ctrl.Name = "Space0" & Convert.ToString(i)
                Case 10 To 99
                    Ctrl.Name = "Space" & Convert.ToString(i)
            End Select
            ' --- code to size and position the control ---
            Ctrl.Text = Convert.ToString(i)
            Me.Controls.Add(Ctrl)
        Next

I used buttons for now, placing the value of i on each button, for debugging purposes. However, only the last button gets shown. What can I do to get this to do what I need?
0
NevadaDimitri
Asked:
NevadaDimitri
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
because you create only 1 new button, on the Dim line.
change the code only slightly:

        Dim Ctrl As Button
        Dim i As Integer

        For i = 0 To 99
            Ctrnl  = new Button
            Select Case i
                Case 0 To 9
                    Ctrl.Name = "Space0" & Convert.ToString(i)
                Case 10 To 99
                    Ctrl.Name = "Space" & Convert.ToString(i)
            End Select
            ' --- code to size and position the control ---
            Ctrl.Text = Convert.ToString(i)
            Me.Controls.Add(Ctrl)
        Next
0
 
Arthur_WoodCommented:
Also, since you are not setting the location of the burron on the screen, each and every button will be placed in the same location (one on top of the next), so that the only button that will be seen is the most recently placed (and last) button.  the other 99 buttons are all stacked, on on top of the next, UNDER that last button.

Try this:

        Dim Ctrl As Button
        Dim i As Integer

        For i = 0 To 99
            Ctrl  = new Button
            Select Case i
                Case 0 To 9
                    Ctrl.Name = "Space0" & Convert.ToString(i)
                Case 10 To 99
                    Ctrl.Name = "Space" & Convert.ToString(i)
            End Select
            ' --- code to size and position the control ---
            Ctrl.Text = Convert.ToString(i)
            Ctrl.Left = i\10 * (ctrl.Width + 5)
            Ctrl.Top = (i - 10* (i\10)) * (Ctrl.Height + 10)
            Me.Controls.Add(Ctrl)
        Next


Yes, that is a \  -- This does an Integer divide (the result is the Largest integer that is smaller that or equal to the result of the division).

AW
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Also, the buttons are not going to be very useful without any event handlers attached to them...

Use AddHandler to wire up the Click event of the buttons:

(borrowing from AW's code)

    ...
        Dim Ctrl As Button
        For i As Integer = 0 To 99
            Ctrl  = new Button
            Ctrl.Name = "Space" & i.ToString("00")

            Ctrl.Text = i.ToString()
            Ctrl.Left = i\10 * (ctrl.Width + 5)
            Ctrl.Top = (i - 10* (i\10)) * (Ctrl.Height + 10)
            Me.Controls.Add(Ctrl)
     
            AddHandler Ctrl.Click, AddressOf Me.btn_Click
        Next


    Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim btn As Button
        btn = CType(sender, Button)
        ' now do something with "btn"...
        Debug.Print(btn.Name)
    End Sub
0
 
NevadaDimitriAuthor Commented:
Thank you angelIII. That made it work perfectly.  :)

Arthur_Wood:
Thanks for the input, however, I replaced the code for sizing and positioning with the comment for the time being since it wasn't vital to the solution.

Idle_Mind:
Thank you for your help as well. I see a couple of alternate methods you used for doing things (such as using = i.ToString() rather than = Convert.ToString(i)). I didn't know it could be done that way also.  :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now