Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 30 pictureboxes that are "Picturebox.visible = false".
I have 3 radiobuttons who decide how much pictureboxes that get visible after clicking a button:

C#
private void buttonStart_Click(object sender, EventArgs e)
        {
            if (radioButtonMakkelijk.Checked)
            {
                aantal = 10;
            }
            else if (radioButtonGemiddeld.Checked)
            {
                aantal = 20;
            }
            else if (radioButtonMoeilijk.Checked)
            {
                aantal = 30;
            }
            else
            {
                buttonStart.Enabled = false;
            }

            for (counter = 0; counter < aantal; counter++)
            {
                string Visible = "Picturebox" + counter;
                Visible.Visible = true;
            }

        }


As you can see above I used a for, to make those pictureboxes Visible.. But I dont know how I can actually make them visible cause you can't use ".Visible" on a string.
Posted

I would create a List<PictureBox> which would hold all your PictureBoxes.
Then :

for (int i = 0; i < aantal; i++)
{
   pictureBoxes[i].Visible = true;
}
 
Share this answer
 
Enumerate the child controls in the form and if a control is a picture box, take the desired action.

C#
foreach (Control ctrl in this.Controls)
{
    if (ctrl is PictureBox)
    {
        // take further action on the picture box
    }
}


If your PictureBox controls are inside containers, you'll have to write a recursive method that will accept the parent container, like so:

C#
private void ChangePictureBoxes(Control container)
{
    foreach (Control ctrl in this.Controls)
    {
        if (ctrl.HasChildren)
        {
            ChangePictureBoxes(ctrl);
        }
        else if (ctrl is PictureBox)
        {
            // take further action on the picture box
        }
    }
}


This is really the only way you can do what you want without worrying about control names...
 
Share this answer
 
Comments
IImaartenII 4-Nov-11 10:56am    
Ok but after reading your information, I still dont know how to make my pictureboxes visible. I have a variable that has to decide how many pictureboxes will get visible. I need a way to get this code + your code together

for (counter = 0; counter < ammount; counter++)
{
(picturebox + ammount).Visible = True;
}

I know the code above isn't right but I really have no idea how I can get in to work.. If you post an answer, can you please post it that I dont have to change much, cause I'm not really a pro :)

thanks!
C#
for (counter = 0; counter < aantal; counter++)
            {
                string Visible = "this.PictureBox" + counter;
                this.Controls[Visible].Visible = true;
            }


Try This.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 3-Nov-11 14:59pm    
I don't think this will work. You probably meant to say:string Visible = String.Format("{0}{1}", "PictureBox", counter). And this will only work if the names of said picture boxes are the default ones with no holes in the sequence.
IImaartenII 3-Nov-11 15:06pm    
So if I would do it "your way".. what should the total for-code be?
And yes they are default (pictureBox1, pictureBox2,...)
I also noticed that I first typed "PictureBox", so already changed that too "pictureBox"
Try to add this.Refresh();
and sorry for wrong code change it to :
C#
for (counter = 0; counter < aantal; counter++)
            {
                string Visible = "PictureBox" + counter;
                this.Controls[Visible].Visible = true;
                this.Refresh();
            }
 
Share this answer
 
v2
Comments
IImaartenII 3-Nov-11 15:34pm    
where should I insert this.Refresh(); ?
SercanOzdemir 3-Nov-11 15:39pm    
To For Rotation's in.
SercanOzdemir 3-Nov-11 15:47pm    
If it Works please mark as an answer.
IImaartenII 3-Nov-11 15:53pm    
still testing, there is something wrong with my radiobuttons that = "aantal" what is important for my for-code.
SercanOzdemir 3-Nov-11 16:00pm    
It 's about your other code parts.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900