Hi ,
I have 5 or 6 textboxes and all of them are required ( have required field validators ) .. I want to change their background color to yellow when they are left blank and when the box is filled the backgorund color to white ..Is it possible??? to do in c#
And can I apply same method for login page too!!
Loading

Priya LingeRank #785 · 2.9K reputationPosted Aug 7, 2011, 9:39 PM EDT
Try this,
string a = textBox1.Text;
if (a != String.Empty)
{
textBox1.BackColor = Color.Red;
}
else if (a == String.Empty)
{
textBox1.BackColor = Color.White;
}
else
{
textBox1.BackColor = Color.Black;
}
Hope this will help you.
Thanks.
Nits37 reputationPosted Aug 7, 2011, 10:27 PM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 8, 2011, 12:05 AM EDT
If you put it, say, in the Leave eventhandler, then it should work:
Nits37 reputationPosted Aug 10, 2011, 11:19 AM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 10, 2011, 12:18 PM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Aug 10, 2011, 2:29 PM EDT
Satyapriya NayakRank #47 · 31.5K reputationPosted Aug 10, 2011, 7:33 PM EDT
Here is your desired code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace change_color_of_textbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_Leave(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
textBox1.BackColor = Color.Red;
}
else
{
textBox1.BackColor = Color.White;
}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
e.SuppressKeyPress = true;
}
}
private void textBox2_Leave(object sender, EventArgs e)
{
if (textBox2.Text == "")
{
textBox2.BackColor = Color.Red;
}
else
{
textBox2.BackColor = Color.White;
}
}
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
e.SuppressKeyPress = true;
}
}
private void textBox3_Leave(object sender, EventArgs e)
{
if (textBox3.Text == "")
{
textBox3.BackColor = Color.Red;
}
else
{
textBox3.BackColor = Color.White;
}
}
private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
e.SuppressKeyPress = true;
}
}
}
}
Thanks
If this post helps you mark it as answer
Satyapriya NayakRank #47 · 31.5K reputationPosted Aug 11, 2011, 10:58 AM EDT
If this post helps you mark it as answer
Thanks
Nits37 reputationPosted Aug 13, 2011, 5:38 AM EDT
Satyapriya NayakRank #47 · 31.5K reputationPosted Aug 13, 2011, 6:39 AM EDT
Iam attaching the source code ,please look into it.
Thanks