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
Satyapriya NayakPosted Aug 13, 2011, 10:39 AM
Iam attaching the source code ,please look into it.
Thanks
NitsPosted Aug 13, 2011, 9:38 AM
Satyapriya NayakPosted Aug 11, 2011, 2:58 PM
If this post helps you mark it as answer
Thanks
Satyapriya NayakPosted Aug 10, 2011, 11:33 PM
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
Sam HobbsPosted Aug 10, 2011, 6:29 PM
VulpesPosted Aug 10, 2011, 4:18 PM
NitsPosted Aug 10, 2011, 3:19 PM
VulpesPosted Aug 8, 2011, 4:05 AM
If you put it, say, in the Leave eventhandler, then it should work:
NitsPosted Aug 8, 2011, 2:27 AM
Priya LingePosted Aug 8, 2011, 1:39 AM
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.