"Clean" way to modify the "visible" attribute of a PictureBox

5

I have 5 background images for my application that will be visible to the user's taste. What is the simplest (clean code) way to make the user choose?

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        pictureBox1.Visible = true;
        pictureBox2.Visible = false;
        pictureBox3.Visible = false;
        pictureBox4.Visible = false;
        pictureBox5.Visible = false;
    }
    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        pictureBox2.Visible = true;
        pictureBox1.Visible = false;
        pictureBox3.Visible = false;
        pictureBox4.Visible = false;
        pictureBox5.Visible = false;
    }
    private void radioButton3_CheckedChanged(object sender, EventArgs e)
    {
        pictureBox1.Visible = false;
        pictureBox2.Visible = false;
        pictureBox3.Visible = true;
        pictureBox4.Visible = false;
        pictureBox5.Visible = false;
    }
    private void radioButton4_CheckedChanged(object sender, EventArgs e)
    {
        pictureBox1.Visible = false;
        pictureBox2.Visible = false;
        pictureBox3.Visible = false;
        pictureBox4.Visible = true;
        pictureBox5.Visible = false;
    }
    private void img5_CheckedChanged(object sender, EventArgs e)
    {
        pictureBox1.Visible = false;
        pictureBox2.Visible = false;
        pictureBox3.Visible = false;
        pictureBox4.Visible = false;
        pictureBox5.Visible = true;
    }
    
asked by anonymous 29.05.2014 / 01:47

3 answers

4

So:

private void radioButtonUnico_CheckedChanged(object sender, EventArgs e)
{
    pictureBox1.Visible = false;
    pictureBox2.Visible = false;
    pictureBox3.Visible = false;
    pictureBox4.Visible = false;
    pictureBox5.Visible = false;

    switch (((RadioButton)sender).Name) {
        case "radioButton1": 
            pictureBox1.Visible = true;
            break;
        case "radioButton2": 
            pictureBox2.Visible = true;
            break;
        case "radioButton3": 
            pictureBox3.Visible = true;
            break;
        case "radioButton4": 
            pictureBox4.Visible = true;
            break;
        case "radioButton5": 
            pictureBox5.Visible = true;
            break;
    }
}

Set the event for all your RadioButtons .

    
29.05.2014 / 02:12
7

You can get Everyone ( if this is not a problem for you ) components of type PictureBox and place them in a collection , and in a loop change the Visible property to false , and then to PictureBox target > change to true the Visible property . Something like this:

   var pictureBoxes = Controls.OfType<PictureBox>();
   foreach (var p in pictureBoxes) p.Visible = false;
   pictureBox1.Visible = true;

Do this in the event CheckedChanged() of all your RadioButton's just changing the name of the pictureBox target.

    
29.05.2014 / 02:38
7

Morrison Mendez's Answer is good and solves the problem. But someone may have a problem where they can not change the structure of the methods. Still has a solution:

private void ChangePictureBoxes() {
    pictureBox1.Visible = false;
    pictureBox2.Visible = false;
    pictureBox3.Visible = false;
    pictureBox4.Visible = false;
    pictureBox5.Visible = false;
}

private void radioButton1_CheckedChanged(object sender, EventArgs e) {
    ChangePictureBoxes();
    pictureBox1.Visible = true;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e) {
    ChangePictureBoxes()
    pictureBox2.Visible = true;
}
private void radioButton3_CheckedChanged(object sender, EventArgs e) {
    ChangePictureBoxes()
    pictureBox3.Visible = true;
}
private void radioButton4_CheckedChanged(object sender, EventArgs e) {
    ChangePictureBoxes()
    pictureBox4.Visible = true;
}
private void img5_CheckedChanged(object sender, EventArgs e) {
    ChangePictureBoxes()
    pictureBox5.Visible = true;
}

Eventually you can use the utility method the way Sunstreaker suggested, as long as you know that all PictureBox should get that state. In fact, if the most appropriate semantics is "all PictureBox s" rather than " PictureBox s such and such", this would be a more generic and lasting solution, although possibly having a loss of performance but probably irrelevant.

private void ChangePictureBoxes() {
    foreach (var p in Controls.OfType<PictureBox>()) p.Visible = false;
}

I've placed it on GitHub for future reference .

I'm not a fan of the term attribute in this context, I prefer field and show because in What is the difference between attribute and field in classes ? .

    
29.05.2014 / 06:22