0

I wan't to create a bunch of variables inside a While, each one with different names.

Here is what i tried:

 Dim asd As Integer = 1
    While asd < 5
        Dim picturebox +asd As New Picturebox
    End While

I want that it creates the Picturebox1 Picturebox2 ... and so on, but the "asd" variable won't evaluate and the code won't work. How could you create variables with different names in a loop with Visual Studio?

4
  • 1
    Why don't you use an array? picturebox[3] = new PictureBox()? Commented May 10, 2014 at 18:29
  • probably a good idea to add them to a form.Controls somewhere rather than just create them en masse. Its the 21st century, we have robot dogs and flying cars, so no need to use an old fashioned array, use a List(of PictureBox) if you need to store them anywhere besides a Controls collection. Commented May 10, 2014 at 18:46
  • @Plutonix FYI: The items in a List<T> are actually stored in an (old fashioned) Array<T> Besides that, I totally agree with you;) Commented May 10, 2014 at 18:56
  • Its worth noting that the name that you give to your variables inside your loop is irrelevant. Once the loop finishes you can't reference the variables by the names you've given them, you would need to find them in whatever collection you added them to in order to do anything with them. Commented May 10, 2014 at 19:16

2 Answers 2

1

What I've done in the past is something like:

For i = 0 to 5
    Dim t As New PictureBox()
    t.Name = "PictureBox" & i
    Me.Controls.Add(t)
Next
Dim picToChange = From r in Me.Controls Where Typeof(r) Is PictureBox AndAlso r.Name = "PictureBox1" Select r
If picToChange IsNot Nothing AndAlso picToChange.Any Then
    'Do Something
End If

This is a very basic example and your linq would probably be more dynamic than the one I used but you should get the idea. In this case I'm assuming that you are just putting the PictureBoxes on the form, if this isn't the case then you will need to linq through whichever collection you are adding the controls to.

Edit #1:

As far as events are concerned you will need to add the handlers manually. So your code would become:

For i = 0 to 5
    Dim t As New PictureBox()
    t.Name = "PictureBox" & i
    AddHandler t.Click, AddressOf(FunctionToHandleClick)
    Me.Controls.Add(t)
Next
Dim picToChange = From r in Me.Controls Where Typeof(r) Is PictureBox AndAlso r.Name = "PictureBox1" Select r
If picToChange IsNot Nothing AndAlso picToChange.Any Then
    'Do Something
End If

And the FunctionToHandleClick would look like this:

Private Sub FunctionToHandleClick(ByVal sender As Object, ByVal e As ClickEventArgs)
End Sub
Sign up to request clarification or add additional context in comments.

9 Comments

The code inside the i loop can be reduced to one line: Me.Controls.Add(New PictureBox() With {.Name = "PictureBox" & i}) ;)
@Bjørn-RogerKringsjå very true, I was trying to be as explicit as possible though, since the asker seems new to VB...
Yeah, I just thought it was worth mentioning;)
In case that i need a .click event on the created picturebox, what im supposed to do? Handles t.click gives an error
@user1938775 sender is going to be the picturebox firing the event. when you create it give it a good name ("pictureBox1" is NOT) and/or use the .Tag property so that Ctype(Sender, PictureBox).Name or .Tag give you the info you need.
|
0

Why don't you use an Array?

Dim pictureboxes(5) As PictureBox

For i As Integer = 0 To 5
    pictureboxes(i) = New PictureBox()
Next

2 Comments

Edited. I was being silly and posted it in C# syntax.
and what if the amount of picture boxes i need to create is a variable too, is it possible? dim pictureboxes(variable) As Picturebox

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.