Jasmine Appelblad
Jasmine Appelblad

Reputation: 1600

Push Button (On/Off) in C# Windows Forms

On the Windows Form application I have a Lamp image (a black and white one, and a bright one. For OFF and ON respectively).

Using the Button how can I achieve the scenario such that same button will turn the property of the image (pictureBox in my case) to show the Lamp as ON and pressing the same button again will turn the Lamp off.

I am accessing the 'Visible' property of picture box.

Upvotes: 2

Views: 10446

Answers (3)

Jurgen D
Jurgen D

Reputation: 11

A bit late to the party, but you can use a checkbox and set the appearance to button. I think that would do what is expected by the original post.

Upvotes: 1

Michael Bahig
Michael Bahig

Reputation: 766

I'm not sure about the way to put 2 images over each other, but if you want to reach the same effect:

  • place the 2 image files in your project resources
  • in the click event of the button, toggle the button image depending on a setting:

this would be in the click event:

Properties.Settings.Default.IsOptimizedForTracer !=Properties.Settings.Default.IsOptimizedForTracer;

if (!Properties.Settings.Default.IsOptimizedForTracer)
{
btnOptimizeForTracer.Image = Properties.Resources.TracerOFF;
return;
}

btnOptimizeForTracer.Image = Properties.Resources.TracerON;

Upvotes: 0

Anders Abel
Anders Abel

Reputation: 69280

Put two images on top of each other and get the button to switch which one of them is enabled.

In the form designer you make one of them visible and the other non-visible. The code in the button handler can then be something like:

lightImage.Visible = !lightImage.Visible;
darkImage.Visible = != lightImage.Visible;

That will swap which one is visible and eliminate the need to keep state elsewhere.

Upvotes: 3

Related Questions