Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am creating a treasure hunt game using visual studio and try to hide the image in a button. Someone please help me

What I have tried:

I have tried to some codes like: btn.Visible = false;
but have not worked
Posted
Updated 18-May-16 21:18pm
v2
Comments
Sergey Alexandrovich Kryukov 18-May-16 20:55pm    
"Object on a button"? Didn't you misspell something? Below, you mention hiding an image.

Do you really want to hide image, not button? Why then you hide the button? Do you just mean that a button should have a picture on it, which should be sometimes shown, sometimes not?

Such problems are very simple, but to get help, you need to answer: what do you mean by "Button"? Which one? Full type name, please.
Also, it will be helpful to show the code where you show an image in a button.

—SA

1 solution

We can't really help you that much here, because we have no idea how you are implementing your objects the user is searching for. But by the sound of it, you are implementing them as buttons with an image on them - which isn't a brilliant way to do it - and so setting the Visible property to false should work, provided it's the right instance of the button, and not a new one you just created. If you are saying
C#
Button btn = new Button();
btn.Visible = false;
Then that won't work because you aren't affecting the actual button that the user is looking at and interacting with.
Exactly how to implement this game will depend on the environment you are running in: a WinForms solution would be very different from a web-based solution for example.
But...for I'd try not to use buttons at all.
For example, for WinForms, I'd draw the background onto a Panel using the BackgroundImage property, and handle the Paint event to "add" the objects on top of that image. I'd then handle the MouseClick event for the panel and check if the user was inside one of the objects. If so, remove it from the "to be drawn" list and Invalidate the panel to force a redraw. By setting Form.DoubleBuffered to true the use shouldn't see anything happen.
There are a lot of ways to make the "hit testing" easier: there is a GraphicsPath class which can describe where your object is and what shape it is, and it has a IsVisible method which checks a Point to see if it's inside it or not.
It's a bit more work, but it's going to produce a better result than buttons!
 
Share this answer
 

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