简体   繁体   English

从颜色对话框禁用颜色

[英]disable a color from color dialog

i want to allow users to pick a background color for my program using a ColorDialog but as my label's text is black in color, I want to make the user not able to pick black from the color dialog so that the color will not overlap my label text color. 我想允许用户使用ColorDialog为我的程序选择背景颜色,但是由于我标签的文本是黑色,我想让用户无法从颜色对话框中选择黑色,以使颜色不会与我的标签重叠文字颜色。 is there any way I can do that? 有什么办法可以做到吗? I have also thought of bringing up an error message if the user selects black as shown below but after the color dialog comes up again the color I selected does not becomes the BackColor of my form 我还考虑过如果用户选择黑色,则显示错误消息,如下所示,但是再次出现颜色对话框后,我选择的颜色不会成为表单的BackColor

if (color.ShowDialog() == DialogResult.OK)
{
    if(color.Color == Color.Black)
    {
        MessageBox.Show("Color cannot be black", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        color.ShowDialog();
    }
    else
    {
        BackColor = color.Color;
        backColor = color.Color;
    }
}

Try to use while instead of if when checking for color: 尝试使用while而不是if来检查颜色:

if (color.ShowDialog() == DialogResult.OK)
{
    while(color.Color == Color.Black)
    {
        MessageBox.Show("Color cannot be black", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        color.ShowDialog();
    }
    BackColor = color.Color;
    backColor = color.Color;
}

This way, unless you pick a color that's not black, it will show an error message and open the ColorDialog again. 这样,除非您选择非黑色的颜色,否则它将显示错误消息并再次打开ColorDialog。 As to why the color wouldn't change for you after you reopened the dialog, it is because you are changing the background color only in the else clause, therefore if you at first chose black the BackColor wouldn't change. 至于为什么在重新打开对话框后颜色不会为您更改,这是因为仅在else子句中更改了背景颜色,因此,如果首先选择黑色,则BackColor不会更改。 Another way to solve your problem is by changing the color of your labels to white if the chosen color is black(don't forget to change the label text back to black when another color is chosen). 解决问题的另一种方法是,如果选择的颜色是黑色,则将标签的颜色更改为白色(当选择其他颜色时,请不要忘记将标签文本更改为黑色)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM