0

I have a quick question, I have been struggling with something that should, in theory be quite simple.

I want to create a dynamic button out of a picture box in Visual Studio 2012, one that changes the image each time I click it.

if (pictureBox4.BackgroundImage == MyProject.Properties.Resources._1)
    pictureBox4.BackgroundImage = MyProject.Properties.Resources._2;
else if (pictureBox4.BackgroundImage == MyProject.Properties.Resources._2)
    pictureBox4.BackgroundImage = MyProject.Properties.Resources._1;

Now, this didn't really work fine. It wouldn't detect the image that is being currently displayed and enter the if statement. So, instead, I've tested it this way.

int b = 1; 

if (b == 1)
{
    pictureBox4.BackgroundImage = MyProject.Properties.Resources._2;
    b = 2;
}

if (b == 2)
{
    pictureBox4.BackgroundImage = MyProject.Properties.Resources._1;
    b = 1;
}

Close... but no cigar. When I click it the image does change, but just once; if I click it again, it stays the same...

So... what now? Thanks for your answers.

Emperor XLII
  • 13,014
  • 11
  • 65
  • 75
silviussa
  • 1
  • 1
  • 7
  • Is the first line (int b = 1;) inside your event handler method or is it global to the class? – Matt Varblow Jan 31 '15 at 20:36
  • global, in the class, sorry, i forgot to specify that, it would have been quite dumb of me to put it inside the event handler... hehe – silviussa Jan 31 '15 at 20:42
  • add an else clause before each if or else it won't work ;-) (because the first change will make the second if true and it'll revert that change etc..) – TaW Jan 31 '15 at 20:47
  • Are you really using this line in both if clauses? pictureBox4.BackgroundImage = MyProject.Properties.Resources._2; – Matt Varblow Jan 31 '15 at 20:47

2 Answers2

1

You're using the same image in both if clauses (Resources._2)?

Also, as TaW pointed out, when b == 1 you set b = 2 and check if b == 2. Both if clauses will be true. The second if should be an "else if"

    if (b == 1)
    {
        pictureBox4.BackgroundImage = MyProject.Properties.Resources._2;
        b = 2;
    }
    else if (b == 2)
    {   
        pictureBox4.BackgroundImage = MyProject.Properties.Resources._1;
        b = 1;
    }

Of course you could just use an else clause:

    if (b == 1)
    {
        pictureBox4.BackgroundImage = MyProject.Properties.Resources._2;
        b = 2;
    }
    else
    {   
        pictureBox4.BackgroundImage = MyProject.Properties.Resources._1;
        b = 1;
    }

Or if you don't want the integer variable then you could try this:

1) Pull the images from the resource just once and them in a static member.

    private static readonly Image Image1 = MyProject.Properties.Resources._1;
    private static readonly Image Image2 = MyProject.Properties.Resources._2;

2) Change your conditions to use the static copies instead of the resource properties (which return a new object each time).

    if (pictureBox4.BackgroundImage == Image2)
    {
        pictureBox4.BackgroundImage = Image1;
    }
    else
    {
        pictureBox4.BackgroundImage = Image2;
    }
Matt Varblow
  • 7,651
  • 3
  • 34
  • 44
  • a type probably. More importantly he is missing the else clause! – TaW Jan 31 '15 at 20:54
  • Yep, sorry, I meant Resources._1 my bad... now, strangely enough, i ran the code again, and it doesn't work at all, not even first click... – silviussa Jan 31 '15 at 20:57
  • Hehe. Read our comments again ;-) – TaW Jan 31 '15 at 21:00
  • You replaced the second if with an else? (See my edited answer and the comment from TaW.) If you missed that then it wouldn't update at all. – Matt Varblow Jan 31 '15 at 21:03
  • ok, that was quite dumb of me, not putting the else there... that fixed the problem! so it works... buuuuut, i will be putting many of these in a row, i cant afford to create 20 integers for every one, any ideas why isnt the first method working?? – silviussa Jan 31 '15 at 21:05
  • it never detected that pictureBox4.BackgroundImage = MyProject.Properties.Resources._1; and switched it to MyProject.Properties.Resources._2; should i use another function? – silviussa Jan 31 '15 at 21:08
  • Ah, now we're talking.. - You should load the images into an array and then keep incementing the index (until it hits the count, then set back to 0). If your image have the same size and are less than 256x256 pixels in size an ImageList would be a nice place to store them.. No need for an if there.. – TaW Jan 31 '15 at 21:10
  • Your original approach didn't work because of the double ifs. With an else it should work. I edited my answer again to reflect this. – Matt Varblow Jan 31 '15 at 21:12
  • Do have a look at [this link](http://stackoverflow.com/questions/4536112/c-sharp-resource-array) to learn how to access your resources as an array!! – TaW Jan 31 '15 at 21:13
  • Thanks TaW, ill take a look at that. Tho, the problem relies on not being able to even enter the IF statement.... Matt Varblow, i did add the if else (it was actually there to begin with, i just miss typed it)... and still doesn't work, it doesn't work even if I write only one IF statement instead of both to test it out... – silviussa Jan 31 '15 at 21:17
  • Every time you reference the resource it constructs a new object. So the straight image equality check will always fail. You'll have to compare some property of the image. The Tag property is a good one. See my updated answer for an example. – Matt Varblow Jan 31 '15 at 21:39
  • Actually, don't bother with that... I tested. It suffers from the same problem. The tag is set on the object, but a new object is created each time so it doesn't stick. You might just want to stick with the integers. Probably in an array as TaW suggested. – Matt Varblow Jan 31 '15 at 21:44
  • For my purpose the integer solution may actually be easier... tho TaW's solution is more efficient, well thanks for your help anyways Matt Varblow! – silviussa Jan 31 '15 at 21:48
1

Here is my solution for the task: In MyProject I have a resource file called Resource1 and six Images from called _0 to _5:

int index = 0;
private void anImageButton_Click(object sender, EventArgs e)
{
    index = (index + 1) % 6;
    anImageButton.Image = 
          (Bitmap)MyProject.Resource1.ResourceManager.GetObject("_" + index);
}

Instead of a PictureBox I use a simple Button anImageButton, but it will work with your PictureBox as well..

TaW
  • 53,122
  • 8
  • 69
  • 111