Return original Textbox formatting

Asked

Viewed 114 times

3

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

At the moment, to return, 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;
            }
        }
    }

However, this method brings me problems, because I have some Textbox that has the parameter ReadOnly = true, that when used leaves the Textbox with the gray background color, and when I run the method above, all Textbox have the white background.

2 answers

5

You can also use the 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;
        }
    }
}
  • 1

    That’s right @Julio Borges, the use of system colors is always more appropriate, as long as you don’t need a specific color, because with the system colors the application will follow the color theme of the environment in which it is running. + 1

  • 1

    I certainly responded to enrich the content of your reply without editing it.

  • 1

    Julio and Emerson, Perfect. Thank you so much. I tested it here and it works perfectly. Thank you

3


You can add one more test in your routine, to change the background color only of TextBox who is not with the property ReadOnly configured for 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; 
                } 

            }
        }
    }
  • Friend, thanks for the answer, now a doubt, pq uses the ! in front of ((Textbox)(Ctrl)). Readonly) ?

  • 1

    This way it works, more vc can also use 'System.Drawing.Systemcolors.Window' for normal Textboxes and 'System.Drawing.Systemcolors.Control' for Readonly Textboxes, because in this way if there is any change of user theme the system color will be used in a standardized way.

  • Thomas the ! is the operator of denial, the not of other languages. How ReadOnly is a property Boolean we can use its value to test a condition of a if, for example. To make it clearer we could do so: if (((TextBox)(ctrl)).ReadOnly!=true) or so if (((TextBox)(ctrl)).ReadOnly==false).

  • Perfect. Thank you very much. = D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.