Simon
Simon

Reputation: 25993

How do I get a disabled ToolStripButton to paint its image in colour?

We have a button which allows users to 'lock' a form. Users are not permitted to 'unlock' the form, so when pressed, we want the button to be disabled, so that the user receives appropriate visual feedback.

However, the customer reports that the greyed 'lock' icon suggests to them that the form is not locked, so we would like to display the button in a pressed state, but with the icon in colour, even though the button is disabled.

How do we do that, with the absolute minimum amount of overridden painting?

Upvotes: 1

Views: 3057

Answers (4)

TheMightyX2Y
TheMightyX2Y

Reputation: 1503

I was in this case few weeks ago and this question doesn't have an answer, so this is my solution 4 years later :

I'm using this solution to repaint the ToolStripButton in many cases : disabled, checked, selected

  1. override OnPaint event or change renderer of your ToolStrip
  2. draw your image without grey filter with this method, just set the image of the button and image's position on your button : http://msdn.microsoft.com/en-us/library/chas0s9c(v=vs.110).aspx

Two good examples who helped me :

ToolStrip class :

ToolStrip ts = new ToolStrip();
//[...]
ts.RenderMode = ToolStripRenderMode.Professional; //Professional is just an example
ts.Renderer = new CustomRenderer();

CustomRenderer class :

public class CustomRenderer: ToolStripProfessionalRenderer
{
    protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
    {
        if (!e.Item.Enabled) //in this case you will just have the image in your button, you need to add text etc in this if
        {
             //to draw the image
             Graphics g = e.Graphics;
             g.DrawImageUnscaled(e.Item.Image, new Point(2, 2)); //you need to specify the correct position
        }
        else //other cases
            base.OnRenderButtonBackground(e);
     }
}

The problem is to know the correct position of all elements in your ToolStripButton , that's why I drawn the ToolStripButton in every cases

Upvotes: 1

Michaël Carpentier
Michaël Carpentier

Reputation: 2135

You can set the ToolStripButton Image as BackgroundImage
and then set the DiplayStyle to None.

The picture should stay in colour no matter what's the button Enabled value.

Upvotes: 1

G S
G S

Reputation: 36858

Another way: don't set the button.Enabled = false at all. Just set the button.Checked = true or false and when someone clicks on the button test for the Checked state and do whatever you do. The button will then function as you want and stay colored too.

Upvotes: 0

Neil Barnwell
Neil Barnwell

Reputation: 42165

Actually I disagree with the approach. Your customer might have a valid point, but I don't think they have the correct suggestion for the fix. You should look into other ways to make the form appear "locked". Changing borders or font colours, having a big padlock icon appear or change from open to closed etc. Buttons look a certain way because that's what users expect. If you have a disabled button that looks like it might be enabled, that's going to confuse users who might not then understand why they can't click it.

Upvotes: 1

Related Questions