views:

92

answers:

1

I've recently started a WinForms project in Visual Studio 2008 and I notice that when I disable a command button, it flattens out the button and leave the text black. This is quite an unexpected change from my experiences in VB6 and VS2005 where the button simply grays itself out.

Is there a setting that would allow me to make the disabled state of the command button 3D like the enabled state? I have the FlatStyle set to Standard, though System results in the same behavior. I also noticed that UseVisualStyleBackColor needs to be set to True for the button to appear correctly in its Enabled state.

Any help is greatly appreciated.

+1  A: 

The painting code for a button is locked up tight in internal classes, you can't override it nor re-use it. The public ButtonRenderer class doesn't handle disabled buttons.

One thing that could work is playing games with the BackgroundImage property. Add a new class to your project and paste the code shown below. Drop the new control from the top of the toolbar onto your form.

using System;
using System.Drawing;
using System.Windows.Forms;

public class MyButton : Button {
  protected override void OnSizeChanged(EventArgs e) {
    base.OnSizeChanged(e);
    Bitmap bmp = new Bitmap(this.Width, this.Height);
    using (Graphics gr = Graphics.FromImage(bmp)) {
      ButtonRenderer.DrawButton(gr,
        new Rectangle(0, 0, bmp.Width, bmp.Height),
        System.Windows.Forms.VisualStyles.PushButtonState.Normal);
    }
    if (this.BackgroundImage != null) this.BackgroundImage.Dispose();
    this.BackgroundImage = bmp;
  }
}
Hans Passant
It's very close. The new button is indeed 3D in the disabled state, but has a strange white border around it, almost as if the standard state image is being overlayed on the disabled state. However, with a little toying I think it will work perfectly.Thanks!
Shaun Hamman