1

The title may not accurately explain what i would like to do,

Dim BarName As String
For i = 0 To 20
  BarName = "Graph1Bar" And CStr(i)
  Dim BarName As New PictureBox

Next

I would like to create a new picture box for every instance of a for loop, and intend to change their names based on what iteration of the loop they were made in. Is there any way to do this?

I would also like to be able to call on these variables in a loop, like this:

For i = 0 to 20
  Graph1Bar(i).Height = 200
Next

I'm not sure if any of this is possible but some help or explanation would be greatly appreciated!

6
  • 3
    Obviously, you can't have a variable be a string and a PictureBox. Just set the Name property of the control to be able to reference it in the Controls collection of the parent container. Me.Controls("Graph1Bar20")... Commented Oct 15, 2018 at 20:35
  • Four space indent to get code format at this site. Commented Oct 15, 2018 at 20:37
  • Use a List(Of PictureBox). Iterate the List items, add a Name, set the Size, add to the Form.Controls collection. You can then use the List Items to control your Controls. Commented Oct 15, 2018 at 20:38
  • I think as you've shown in the second code block, just define an array (either fixed or dynamically resizable) Commented Oct 15, 2018 at 20:58
  • 1
    What you're suggesting would be useless anyway, because your variable would just be a local and cease to exist as soon as it goes out of scope. Commented Oct 15, 2018 at 22:54

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.