|
-
May 9th, 2012, 03:25 PM
#1
Thread Starter
Addicted Member
Using dim to for picturebox events
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
-
May 9th, 2012, 03:52 PM
#2
Re: Using dim to for picturebox events
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
-
May 9th, 2012, 04:24 PM
#3
Re: Using dim to for picturebox events
If all the pictureboxes are in the same controls collection, you can access them like:
Code:
For i = 0 To 10
DirectCast(<some container>.Controls("PictureBox " & i.toString),Picturebox).Image = <whatever>
Next
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.
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.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|