Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

c# - How to avoid color changes when button is disabled?

We have a Windows Forms project with quite a few FlatStyle buttons.

When we disable the buttons, the colors of the buttons are changed automatically Frown | :(

Is it possible to override this somehow, so we can control the colors ourselves?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use the EnabledChanged event to set the desired color. Here is an example.

private void Button1_EnabledChanged(object sender, System.EventArgs e)
{
Button1.ForeColor = sender.enabled == false ? Color.Blue : Color.Red;
Button1.BackColor = Color.AliceBlue;
}

Use the desired colors according to your requirement.

Also you need to use the paint event.

private void Button1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
dynamic btn = (Button)sender;
dynamic drawBrush = new SolidBrush(btn.ForeColor);
dynamic sf = new StringFormat {
    Alignment = StringAlignment.Center,
    LineAlignment = StringAlignment.Center };
Button1.Text = string.Empty;
e.Graphics.DrawString("Button1", btn.Font, drawBrush, e.ClipRectangle, sf);
drawBrush.Dispose();
sf.Dispose();

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...