Click here to Skip to main content
15,860,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to change textBox1 border color if textBox1 is empty then textBox1 border color should be Red


What I have tried:

if (textBox1.Text.Length > 0)
       {

       }
      else
       {
        textBox1.Focus();
        textBox1.BorderColor = Color.Red;
        textBox1.BorderColor = System.Drawing.Color.Red;
        }
Posted
Updated 23-Aug-20 22:57pm
Comments
CHill60 24-Aug-20 4:34am    
And what went wrong when you ran this code?
Where is this code - what is triggering it?
(You do not need to give the textbox focus)
Is this Winforms, WPF, ASP.net?
Sandeep Mewara 24-Aug-20 4:57am    
+ you trying to have it by default or when the form/page is submitted?

1 solution

The only time a textbox has a BorderColor property is when it's a web-based control, and that means you are doing the validation in the wrong place: you should do this in Javascript at the Client, not in C# at eth Server - otherwise every time the user types in the TextBox, you have to do a round trip to the server!
That's slow, and can really slug your UI! It also can load your server considerably if you have a lot of users.

Always do all primary validation at the client, not the server.

For non web-based controls, you have to get more creative: c# - Change the borderColor of the TextBox - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900