Return original formatting TextBox

3

I have a method, which checks if a TextBox is filled, if it is normal, if it is blank, it displays a message on the screen, and it paints the background of the TextBox in yellow, Here comes my doubt, how do I return to the default color?

Currently, to go back, I'm using:

 public void limparCorBoxes(Control.ControlCollection controles)
    {
        //Faz um laço para todos os controles passados no parâmetro
        foreach (Control ctrl in controles)
        {
            //Se o contorle for um TextBox...
            if (ctrl is TextBox)
            {
                ((TextBox)(ctrl)).BackColor = System.Drawing.Color.White;
            }
        }
    }

But this method brings me problems, as I have some TextBox that has the parameter ReadOnly = true , which when used leaves the TextBox with the gray background color, and when I execute the method above, all Textbox are with the white background.

    
asked by anonymous 28.06.2016 / 05:55

2 answers

3

You can add another test in your routine to change the background color only of TextBox which is not with the ReadOnly property set to true .

See if it works:

 public void limparCorBoxes(Control.ControlCollection controles)
    {
        //Faz um laço para todos os controles passados no parâmetro
        foreach (Control ctrl in controles)
        {
            //Se o contorle for um TextBox...
            if (ctrl is TextBox)
            {
                if (!((TextBox)(ctrl)).ReadOnly) {
                   ((TextBox)(ctrl)).BackColor = System.Drawing.Color.White; 
                } 

            }
        }
    }
    
28.06.2016 / 14:02
5

You can also use system colors for better standardization:

public void limparCorBoxes(Control.ControlCollection controles)
{
    //Faz um laço para todos os controles passados no parâmetro
    foreach (Control ctrl in Controls)
    {
        //Se o contorle for um TextBox...
        if (ctrl is TextBox)
        {
            ctrl.BackColor = ((TextBox)ctrl).ReadOnly 
                    ? System.Drawing.SystemColors.Control 
                    : System.Drawing.SystemColors.Window;
        }
    }
}
    
28.06.2016 / 14:23