Resolved Referencing multiple pictureBoxes on a form using an index.

MPIon

Well-known member
Joined
Jun 13, 2020
Messages
73
Location
England
Programming Experience
10+
I have a form with several picture boxes named pictureBox1, pictureBox2, pictureBox3
I can set an individual image in one of the boxes using code :
C#:
       pictureBox1.Image = Image.FromFile(<full path name for image file>);
However, I want to be able to set the image for a specified picture Box using an index value.
(this is easy to do in Visual Basic - just put for example Me("pictureBox" & 3).Picture to set the picture in pictureBox3)

I am sure there must be a simple way to do this in c# and have looked at numerous examples, but can't get any to work - they won't even compile.
e.g.
C#:
       Controls["pictureBox" + 3.ToString()].Image = Image.FromFile(<full path name for image file>);

1691335542442.png


Can anyone help with this?
 
When you index the Controls collection, you get back a Control reference, because it can be any type of control. As the error message says, Image is not a member of the Control class. It's a member of the PictureBox class, so you need to cast as that type in order to refer to members of that type:
C#:
((PictureBox)Controls["pictureBox" + 3]).Image = Image.FromFile(<full path name for image file>);
Note that I have omitted the pointless ToString call. The addition operator for types string and int is implemented as concatenation.
 
When you index the Controls collection, you get back a Control reference, because it can be any type of control. As the error message says, Image is not a member of the Control class. It's a member of the PictureBox class, so you need to cast as that type in order to refer to members of that type:
C#:
((PictureBox)Controls["pictureBox" + 3]).Image = Image.FromFile(<full path name for image file>);
Note that I have omitted the pointless ToString call. The addition operator for types string and int is implemented as concatenation.

Thanks for the reply, but unfortunately that did not work - I had seen examples of that but the message was the same.
C#:
          ((PictureBox)Controls["pictureBox" + 3].Image = Image.FromFile(<full path name for image file>);

1691343551335.png


Thanks for the info about the unneccessary ToString call.

In the absence of a simple fix for this, I am implementing a method of creating the picture boxes in code using :-
C#:
        static readonly PictureBox[] images = new PictureBox[4];
        // Create 4 picture boxes.
        for (int i = 0; i < 4; i++)
        {
            images[i] = new PictureBox
            {
                Location = new System.Drawing.Point(250 + 320 * i, 50)
            };
            Controls.Add(images[i]);
            images[i].Size = new System.Drawing.Size(300, 200);
            images[i].Visible = true;
            images[i].Enabled = true;
            images[i].SizeMode = PictureBoxSizeMode.Zoom;
            images[i].BringToFront();
        }

This does seem to work and I can add the images later using an index into images.
 
VB (not VB.NET) is late bound. That's why it can do something dynamic like that. C# is early bound (to except when you are using dynamic variables).

I was actually going to suggest the array approach your implemented.

The code from post #2 is correct. The problem is that your code in post #3 has the parenthesis at the wrong place. Post #2 has this:
C#:
((...)Controls[...]).Image ...
but you wrote:

C#:
((...)Controls[...].Image) ...
 
VB (not VB.NET) is late bound. That's why it can do something dynamic like that. C# is early bound (to except when you are using dynamic variables).

I was actually going to suggest the array approach your implemented.

The code from post #2 is correct. The problem is that your code in post #3 has the parenthesis at the wrong place. Post #2 has this:
C#:
((...)Controls[...]).Image ...
but you wrote:

C#:
((...)Controls[...].Image) ...

Yes, well spotted. I did have it wrong, despite trying several times and having copied and pasted examples.

However, I will probably go with the array approach, now that I have got that to work.

Thanks again.
 
I will probably go with the array approach, now that I have got that to work.

In proper programming languages, like C#, any time that you have some variables named like:

PictureBox1
PictureBox2
PictureBox3

And you want to access them by index, then you want an array. The end.

Yea, there are other fiddles you can do with windows forms, naming the controls, searching them by name that is composed..
..but in a general sense that works universally, the array approach is correct
 
In proper programming languages, like C#, any time that you have some variables named like:

PictureBox1
PictureBox2
PictureBox3

And you want to access them by index, then you want an array. The end.

Yea, there are other fiddles you can do with windows forms, naming the controls, searching them by name that is composed..
..but in a general sense that works universally, the array approach is correct

Thanks for the reply. Yes, I have gone for array approach.
 

Latest posts

Back
Top Bottom