1

Right, in Windows Forms, I've created a grid of 100 picture boxes. Once a picture box is clicked it will turn green, if clicked again it will turn back to the initial colour.

In order to save these I've put each picture box state into an array of bool[100]. These will be saved in XML using serialization. (true if green, false if white(default)).

When loading I am going to need to use the array of bool and define each picturebox's state. The picture box's names are pictureBox1, pictureBox2, pictureBox3 etc. Is there a convenient way of doing this?

Maybe you can understand me better like this:

for (int i = 0; i < 100; i++) {
    if (pictureBoxes[i] == true) //pictureBoxes[] is the bool[] loaded
    { 
       **pictureBox+i**  .BackColor== System.Drawing.Color.LawnGreen; 
    } 
    else if (pictureBoxes[i] == false)
    {
         //System.Drawing.Color.White;
    }
}

This example does not work; Please help?

2
  • 3
    You should not have created the picture boxes using the designer, but using a for loop in the form's constructor (or a Load event). Then you could store them all in a picture box array and access it like pictureBoxControls[i]. Commented Mar 21, 2011 at 18:24
  • Why not just use the background colour as the "boolean"? ie. if (picturebox[i].BackColor == System.Drawing.Color.LawnGreen) { (See the answers for how to actually get the object.) Commented Mar 21, 2011 at 18:29

3 Answers 3

4

Well, the direct answer to your question is to use the indexer by name on the Controls property (a ControlCollection) on the parent-control of the picture-boxes.

Assuming that your code is running in an instance method of the parent (possibly the form), you can do:

// Cast to PictureBox if you need to set a picture-box-specific property.
Controls["pictureBox" + i].BackColor = Color.LawnGreen;

More generally, you can do:

parentControl.Controls["pictureBox" + i].BackColor = Color.LawnGreen;

But how did you get into the strange situation where you have an array of bools with each member associated with a specific picture-box through some naming-convention, in the first place? If you 'manually' dragged a 100 picture-boxes onto the form through the designer, you've probably given yourself an unnecessary headache. It's possible that this could be designed better; with more information we might be able to provide you with a cleaner solution.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply. Im still a beginner in c#. Im trying to make a cinema system and the user is able to choose the seats. the seats are represented by picture boxes.. if taken it will turn green, if not- white. Yes i did manually drag the 100 text boxes.
@user669910: Cheers. Justin's comment is a good starting point. :)
What happens when Controls["pictureBox" + i] doesn't exist? Won't it throw an error?
@Martin: If a control by that name doesn't exist, the indexer returns null. Since the OP has an array of bools with each member implicitly associated with a control, such a scenario would be representative of a bug elsewhere in the OP's program logic.
0

just a suggestion, not sure if it helps(or even works) create a new class that derives from the PictureBox class and extend it by adding the boolean variable. and in the constructor pass the boolean as parameter and change the background color according to it. and dont forget to mark this new class as [Serializable]

Comments

-1

Controls.Find()

This way is actually safer than the rest, because I believe if the control doesn't exist, than an error will be thrown. This method checks to make sure that control exists on the form:

var controls = this.Controls.Find("pictureBox"+i, true);
if (controls.Length > 0)
{
    foreach (var control in controls)
    {
        control.BackColor = Color.LawnGreen;
    }
}

4 Comments

I hope you plan to elaborate.
It's linked, no need to elaborate, right? The page linked explains it.
@Josh M. - If all the OP wanted was a link, they could go to google. One of the greatest benefits of StackOverflow is that it gives you the ability to thoroughly and completely answer a specific question. Posting a single link is, in my opinion, both lazy and goes against what we're trying to do at SO. Also, take for example if Microsoft were to move this page to a different URL tomorrow and someone else was trying to find the answer to this same question. This answer is then of absolutely no help. All it would take to fix it is: Try Controls.Find("pictureBox"+i).BackColor = Color.LawnGreen;
Controls.Find() requires 2 params. The second param is a bool. whether true or false it still aint finding my pictureBox control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.