Disabled control text color - how to change it

While creating my awesome Matching Game;) I found a problem that is completely unattainable.

When a player selects two tags with symbols on them, I want to block all other tags for 4 seconds.

But when I do this, the forecolor of all the shortcuts changes to gray, and the characters are visible. My question is: is there a way to change the ForeColor disabled label in visual C #?

The project is a WinForm application.

At the moment, I set the color of the label in the code as follows:

 label1.ForeColor = lable1.BackColor; 

When a user clicks on a shortcut, I change it to:

 lable1.ForeColor = Color.Black; 
+8
c # winforms label disabled-control
source share
2 answers

Easier than trying to change the way you turn off Windows controls is to simply set a flag if you want Label to be turned off effectively, then check the value of that flag in your Click event handler before taking any action you want. If the control is disabled, do not take any action.

Something like that:

 private bool labelDisabled = false; private void myLabel_Click(object sender, EventArgs e) { if (!labelDisabled) { this.ForeColor = SystemColors.ControlText; } } 

Also note that you should always use SystemColors instead of something like Color.Black .
If you specify specific color values ​​for hard code, they often conflict when the user sets their default Windows theme. Raymond Chen discusses this concern in an article about his blog.

+7
source share

Just create your own shortcut with an overridden paint event:

 protected override void OnPaint ( System.Windows.Forms.PaintEventArgs e ) { if ( Enabled ) { //use normal realization base.OnPaint (e); return; } //custom drawing using ( Brush aBrush = new SolidBrush( "YourCustomDisableColor" ) ) { e.Graphics.DrawString( Text, Font, aBrush, ClientRectangle ); } } 

Be careful with text format flags when drawing text.

+8
source share

All Articles