Hi I have 60 picture boxes on a form, I want to update these images using a for loop. I have tried

Dim PCB as Image    
    For i = 1 to 60
        PCB(i).Image = mySampleProject.My.Resources.TestPCB1
     next

but get an error "Class 'System.Drawing.Image' cannot be indexed because it has no default property"
Does anyone have any idea how to solve this?
Thanks

Recommended Answers

All 3 Replies

All your image boxes are in the Controls array of your form. If you loop that instead, it can work.

You declare PCB as an Image.
You cannot loop through an image that way, as you do in the following lines with PCB(i)

You can loop through all of the pictureboxes by

For Each pbx As PictureBox In Me.Controls.OfType(Of PictureBox)()
    pbx.Image = ...
Next

or if you have named them with the same prefix followed by a number like PictureBox1, PuctureBox2, etc. then you can access them by name as

Dim pbx As PictureBox
.
.
.
pbx = Me.Controls("PictureBox" & i)

as long as you give i a value beforehand. If you are creating code for PictureBox events you can use the same handler for all similar events.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.