开发者

PictureBox at Click

i have create this code for my button but the image doesen't change why?

private void pictureBox94_Click(object sender, EventArgs e)
        {
            if (pictureBox94.Image == Properties.Resources.vuoto)
            {
                pictureBox94.Image = Properties.Resources.select;
                checkBox3.Checked = true;
            }
            else
            {
                pictureBox94.Image = Properties.Resources.vuoto;
               开发者_开发问答 checkBox3.Checked = false;
            }
        }     

any error!


Refactor your method to be check weather the check box is checked instead of checking the image equity:

private void pictureBox94_Click(object sender, EventArgs e)
{
    if (!checkBox3.Checked)
    {
        pictureBox94.Image = Properties.Resources.select;
    }
    else
    {
        pictureBox94.Image = Properties.Resources.vuoto;
    }

    checkBox3.Checked = !checkBox3.Checked;
}


The Problem is, that Properties.Resources.vuoto is implemented as a call to ResourceManager.GetObject (just select it and hit F12 to see the implementation in Resources.Designer.cs), and this call returns a different image instance each time called. Therefore, your if condition is always false.

You can simply test this behaviour by showing the result of

(Properties.Resources.vuoto == Properties.Resources.vuoto)

which also always return false.

So, the easiest way to solve your problem would be to test for the checkBox3.Checked property in your if condition as

if (!checkBox3.Checked) {
  pictureBox94.Image = Properties.Resources.select;
  checkBox3.Checked = true;
} else {
  pictureBox94.Image = Properties.Resources.vuoto;
  checkBox3.Checked = false;
}

Other solutions would be to "cache" the image in object attributes (i.e. create attributes vuoto and select in your Form class and set them once in your constructor), or having an additional boolean flag attribute to store the current state.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜