0

I am trying to compare the background image of previousButton(a button reference) to the background image of Bpown(a button) but the if will not work.

I have tried changing a button's background image to previousButton's BackgroundImage and it worked so it is not connected to the problem.

void click()
        {
            if (turn == true)
            {
                if (previousButton.BackgroundImage == Bpown.BackgroundImage)
                {
                    //unreachable code
                }
            }
        }

EDIT: This is a windows form project

yossico
  • 3,421
  • 5
  • 41
  • 76
  • `==` in this context will compare the references, not the actual images themselves. So if the same image was assigned to both it would be `true`, otherwise it will return false. It won't actually examine the image, if that's what you wanted. – NibblyPig Mar 19 '19 at 14:59
  • This is the same image but thanks anyway :) –  Mar 19 '19 at 15:01
  • 1
    What are you targetting: Winforms, WPF, ASP..? YOU should __always__ TAG your questions correctly so one can see it on the questions page! – TaW Mar 19 '19 at 15:02
  • You say it's the same image, but if you've loaded the same image twice it wouldn't be the same image, this is checking that you've loaded the image once and assigned the same image to both – NibblyPig Mar 19 '19 at 15:03
  • Do not try to compare image content unless you really need to. Instead use the Tags of the buttons to store hints about the images they show! – TaW Mar 19 '19 at 15:04
  • Have a look at these 2 posts: https://codereview.stackexchange.com/questions/39980/determining-if-2-images-are-the-same and https://stackoverflow.com/questions/35151067/algorithm-to-compare-two-images-in-c-sharp – Dimitri Mar 19 '19 at 15:10
  • NibblePig, I already made buttons to contain the backgroundImages so I can go `Bpown.BackgroundImage` to get the images, so I tried setting all the button images with the code like `button1.BackgroundImage = Bpown.BackgroundImage;` but it didn't work. –  Mar 19 '19 at 15:32
  • What does `same` means to you? Same name? Same look? Something else? – Aleksa Ristic Mar 19 '19 at 20:37

0 Answers0