禁用控件的文本颜色 - 如何更改它 [英] Text color of disabled control - how to change it

查看:139
本文介绍了禁用控件的文本颜色 - 如何更改它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建我很棒的配对游戏期间;)我发现了一个完全无法解决的问题.

During the creation of my awesome Matching Game ;) I found a problem that is completely out of reach.

当玩家选择两个带有符号的标签时,我想锁定所有其他标签 4 秒.

When the player chooses two labels with symbols on them I want to lock all the other labels for 4 seconds.

但是当我这样做时,所有标签的前景色都变为灰色并且符号可见.我的问题是 - 有没有一种方法可以更改 Visual c# 中禁用标签的 ForeColor ?

But when I do that, the forecolor of all the labels changes to grey and the symbols are visible. My question is - is there a method to change the ForeColor of a disabled label in visual c#?

该项目是一个 WinForm 应用程序.

The project is a WinForm application.

目前我以这种方式在代码中设置标签的颜色:

At the moment I set the color of a label in code this way:

label1.ForeColor = lable1.BackColor;

当用户点击标签时,我将其更改为:

When the user clicks the label I change it to:

lable1.ForeColor = Color.Black;

推荐答案

方式 比尝试更改 Windows 绘制禁用控件的方式更简单的方法是在需要 Label 时简单地设置一个标志 被有效地禁用",然后在采取任何您想要的操作之前检查您的 Click 事件处理程序方法中该标志的值.如果控件已禁用",则不要执行任何操作.

Way simpler than trying to change the way Windows draws disabled controls is to simply set a flag when you want the Label to be effectively "disabled", and then check the value of that flag in your Click event handler method before taking whatever action you want. If the control has been "disabled", then don't take any action.

类似这样的:

private bool labelDisabled = false;

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

另外,请注意,您应该始终使用 SystemColors 而不是 Color.Black 之类的东西.
如果您对特定颜色值进行硬编码,则当用户自定义其默认 Windows 主题时,它们通常会发生冲突.Raymond Chen 在一个在他的博客上发表文章.

Also, note that you should always use the SystemColors instead of something like Color.Black.
If you hard-code specific color values, they will often conflict when the user customizes their default Windows theme. Raymond Chen discusses the perils of this in an article on his blog.

这篇关于禁用控件的文本颜色 - 如何更改它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆