?
Solved

Using a Variable to Dim a Control

Posted on 2007-10-17
4
Medium Priority
?
1,469 Views
Last Modified: 2013-12-20
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
Comment
Question by:NevadaDimitri
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 800 total points
ID: 20098703
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 20100157
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 20101331
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
 

Author Comment

by:NevadaDimitri
ID: 20103968
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question