Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have 5 TextBoxes and I want to change the border colour of the TextBoxes that are empty. I have a button to save the data of all the TextBoxes and now I want to put a validation in the same button that if a TextBox is empty, it will change the border colour of the respective empty TextBox to Red.

Please help.

What I have tried:

I am unable to write the code as I am unaware of the concept.
Posted
Updated 4-May-18 20:34pm

Taken this is Windows Forms there's no direct way to change the colour of the border. However, you can draw a rectangle above the border to make the control look like the colour has changed.

Have a look at the following discussions:
- c# - Change the borderColor of the TextBox - Stack Overflow[^]
- How to change TextBox border color in C# ?[^]
 
Share this answer
 
1. Put a Panel on your Form, set its BackgroundColor property to the border color you want. Set the Panel's Padding property to the size of the border you want.

2. Paste the TextBox inside the Panel. Set its Dock property to Fill.

You can manipulate the BorderStyle property of both Panel and TextBox to achieve different visual effects.

When you validate (quick sketch):
C#
EmptyTbxBackColor = Color.Red;

foreach (Textbox tbx in MyListOfTextBoxesToBeValidared)
{
   if(tbx.Text == String.Empty) tbx.Parent.BackColor = EmptyTbxBackColor;
}
 
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