I'm coding a project and I need to enable and clear 9 Picture Box's. Is there a way to assign all the picture box's under one variable name so that I can . enable and .image = null all of them at once. Btw i am coding in C# on visual studio windows form app.
3 Answers
Is there a way to assign all the picture box's under one variable name so that I can . enable and .image = null all of them at once
Not quite, but you can do something approaching it if you want to perform the same action on lots of like objects. Store them in an array or List:
var pbs = new PictureBox[]
{
pictureBox1,
pictureBox2,
pictureBox3
/// etc
};
You could then make a method which takes the list and an action to call on them
void ActionOnAll(IEnumerable<PictureBox> pictureBoxes, Action<PictureBox> action)
{
foreach(var pb in pictureBoxes) action(pb);
}
And call it as
ActionOnAll(pbs, pb => pb.Enabled = true);

- 133,658
- 13
- 134
- 193
-
You could use either [Array.ForEach
](https://learn.microsoft.com/en-us/dotnet/api/system.array.foreach?view=net-5.0) or [List – TnTinMn Nov 27 '20 at 02:36.ForEach](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.foreach?view=net-5.0) instead of defining `ActionOnAll`.
A simple way would be to create a class that has a set of methods to operate on Picture Box arrays. For instance you can create a class
class PictureBoxArray
{
private List<PictureBox> myPictureBoxList = null;
public PictureBoxArray()
{
myPictureBoxList = new List<PictureBox>();
}
public PictureBoxArray(params PictureBox[] somePictureBoxes)
{
myPictureBoxList = new List<PictureBox>(somePictureBoxes);
}
public void Add(PictureBox aPictureBox)
{
myPictureBoxList.Add(aPictureBox);
}
public void EnableAndReset()
{
foreach (PictureBox pbx in myPictureBoxList)
{
if (pbx != null)
{
pbx.Enabled = true;
pbx.Image = null;
}
}
}
}
And then use it like
PictureBoxArray myPictureBoxArray1 = new PictureBoxArray(myPictBox1,
myPictBox2,
myPictBox3,
myPictBox4,
myPictBox5,
myPictBox6,
myPictBox7,
myPictBox8,
myPictBox9);
myPictureBoxArray1.EnableAndReset();
or
PictureBoxArray myPictureBoxArray2 = new PictureBoxArray();
myPictureBoxArray2.Add(myPictBox1);
myPictureBoxArray2.Add(myPictBox2, myPictBox3, myPictBox4);
//etc., add all the PictureBox you need to manage
myPictureBoxArray2.EnableAndReset();
Bear in mind that EnableAndReset() must be called from a UI thread. Have a look at Detecting whether on UI thread in WPF and Winforms

- 810
- 5
- 11
To set the image of all pictureboxes, a simple way is using foreach
to traverse to get all pictureboxes.
// define a list to store pictureboxes
// List<PictureBox> pictureBoxes = new List<PictureBox>();
private void btnSetImage_Click(object sender, EventArgs e)
{
foreach (Control control in this.Controls)
{
if (control is PictureBox)
{
// pictureBoxes.Add((PictureBox)control);
((PictureBox)control).Image = null;
}
}
/*
foreach (PictureBox picbox in pictureBoxes)
{
picbox.Image = null;
}
*/
}
Also, you can store them in List<PictureBox>
.(the comment part)

- 3,696
- 3
- 12
- 37