disable a color from color dialog

708 views Asked by At

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. 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

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;
    }
}
1

There are 1 answers

3
YuriKhordal On BEST ANSWER

Try to use while instead of if when checking for color:

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. 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. 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).