Hello, is it possible to
dim i as integer = 1 to 10
dim allpicboxes as *control?* = picturebox("picbox" & i.tostring())
and then use the variable to e.g. change the picboxes images?
so like
allpicboxes.image = my.resources.something
thanks
Printable View
Hello, is it possible to
dim i as integer = 1 to 10
dim allpicboxes as *control?* = picturebox("picbox" & i.tostring())
and then use the variable to e.g. change the picboxes images?
so like
allpicboxes.image = my.resources.something
thanks
Something that I do is I store all the images in a list(of image) and do something like this:
vb Code:
Option Strict On Option Explicit On Public Class Form1 Dim list As New List(Of Image) Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click For i As Integer = list.Count To 0 Step -1 PictureBox1.Image = list.Item(i) Next End Sub End Class
If all the pictureboxes are in the same controls collection, you can access them like:
The key is that all controls have a controls collection. Since the form inherits from Control, it also has a controls collection. Each control is in some controls collection. Therefore, if the pictureboxes are all on the form, then they are all in the forms controls collection. If the pictureboxes are all on a panel, then they are in the panels controls collection, and so on.Code:For i = 0 To 10
DirectCast(<some container>.Controls("PictureBox " & i.toString),Picturebox).Image = <whatever>
Next
Once you know which controls collection the pictureboxes are in, the next thing to note is that controls collections hold Controls. Since Control doesn't have an Image property, you have to cast the control into the right type. Since you know that the control is a picturebox, you can use DirectCast to cast the control into type Picturebox. Once you have done that, you can access any of the picturebox properties. If the properties you want to access are part of Control, such as Name and Location, you wouldn't need to perform the cast.