Button in .NET 2 - possible bug with back color

  • Thread starter Thread starter msw555
  • Start date Start date
M

msw555

I'm encountering strange behavior when modifying the back color of a
button.
Actually when I try to restore the button's original back color I have
the problem. The button does not get back its original back color.
Try the following:
1. Create a new windows forms application.
2. Place a button on the form.
3. In the event handler for the button click write the following code:
this.button1.BackColor = this.button1.BackColor;
4. Run the application and press the button.
5. The color of the button changes, although according to the code you
wrote it should clearly remain the same. At least that's the behavior
I'm getting.

Does anyone have an idea why this happens and / or how to work around
it?

Thanks
 
Hi,

I could reproduce your problem and I think I can explain what is happening.

When you add a button to the form, the property UseVisualStyleBackColor is
true by default.
When you change the BackColor property in your handler, the
UseVisualStyleBackColor is automatically changed to false.
(It also changes when you set the BackColor in the designer).
If you add the line this.button1.UseVisualStyleBackColor = true; after the
line this.button1.BackColor = this.button1.BackColor;,
the background color won't change.

Hope this help.

/Markus
 
Thanks!
Hi,

I could reproduce your problem and I think I can explain what is happening.

When you add a button to the form, the property UseVisualStyleBackColor is
true by default.
When you change the BackColor property in your handler, the
UseVisualStyleBackColor is automatically changed to false.
(It also changes when you set the BackColor in the designer).
If you add the line this.button1.UseVisualStyleBackColor = true; after the
line this.button1.BackColor = this.button1.BackColor;,
the background color won't change.

Hope this help.

/Markus
 
Back
Top