7

How do I toggle between two images on a Button? I have a "Lock" and "Unlock" image I would like to use on the same button.

So far I have used the property window to set a single image..

UPDATE: Many good answers, but I should have mention that my two images is in the Property folder. How do I access them with a relative path?

1
  • Use CheckBox.Appearence=Button and ImageList to set ImageIndex when checkbox is checked. Commented Jan 17, 2012 at 14:06

4 Answers 4

9

You can change a image of Windows Form Button using 2 Methods

Method 1 for Relative Path

button1.Image = System.Drawing.Image.FromFile(@"C:\Users\jk\Desktop\icons\image.png");

button1.Image = Image.FromFile("C:\\Users\\jk\\Desktop\\icons\\image.png");

Method 2 For Resources Image

this.button1.Image = NameSpace1.Properties.Resources.Image2.png;

you can also visit MSDN Library: ButtonBase.Image Property

Sign up to request clarification or add additional context in comments.

Comments

6

Easy:

button1.Image = System.Drawing.Image.FromFile(@"C:\Users\Administrator\Pictures\forestfloor.jpg");

P.S. Before set image, check if it exist

Comments

5

You will most likely have to change the Button.Image property in the code-behind. See the MSDN Documentation for information and a sample on how to do this.

1 Comment

I had to set Button.Image and I used Properties.Resources.lock_open to get to my file. It works perfectly. I only wrote about the toggle function to say that it was a new situation were two images were needed. Sorry for any confusion.
0

You would have to code.

if(locked)
  Button.Image = Images.Lock;
else 
  Button.Image = Images.Unlock;

where Images is, say, a Resource you have created through the designer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.