Link to home
Create AccountLog in
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

Avatar of dontnetsanjay
dontnetsanjay

text box label backcolor change on the fly
Hello,
i have a form in which there are labels and text boxes.
Now wha I am tring to do is when there is focus on a text box(user starts to enter text) back color of text box and label should be changed?
how do we that any code?
Asp.net VS2005
Sanjay

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of hongjunhongjun🇸🇬

In the onfocus event, try this

TextBox1.BackColor = System.Drawing.Color.Red;

Avatar of udhayakumardudhayakumard🇮🇳

        private void textBox1_Enter(object sender, EventArgs e)  // Change Color
        {
            textBox1.BackColor = Color.Yellow;
            label1.BackColor = Color.Yellow;
        }

        private void textBox1_Leave(object sender, EventArgs e) // Back to normal
        {
            textBox1.BackColor = Color.Transparent;
            label1.BackColor = Color.Transparent;
        }

Avatar of hongjunhongjun🇸🇬

It should have been "Enter" event.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of dontnetsanjaydontnetsanjay

ASKER

Hello again,
in my aspx file when i click on text box and then press F4 propeties of that textbox are displayed
and when i switch to event part it does not display any enter or leave event
i typed this code in my aspx.cs file but nothing is happening
thanks

Avatar of udhayakumardudhayakumard🇮🇳

U need to add attributes for the text box

/Server Code
txtName.Attributes.Add("onfocus", "textBox1_Enter();")

txtName.Attributes.Add("onblur", "textBox1_Leave();")

//Client Side Code
function SomeJavaScriptFunction(){
//Hi it's txtName's lost focus calling
//Hi txtName :)
}

ASKER CERTIFIED SOLUTION
Avatar of udhayakumardudhayakumard🇮🇳

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

protected void Page_Load(object sender, EventArgs e)
    {
        TxtCompany.Attributes.Add("onfocus", "txtCompany_Enter();");
         txtCompany.Attributes.Add("onblur", "txtCompany_Leave();");
    }


    private void txtCompany_Enter(object sender, EventArgs e)  
    {
        TxtCompany.BackColor = Color.Yellow;
        lblmsg.BackColor = Color.Yellow;
    }


these lines are givin error when we click on the text box

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of udhayakumardudhayakumard🇮🇳

k forget that sanjay try this

txtEmail.Attributes.Add( "onFocus", "this.style.backgroundColor='aliceblue';" );
  txtEmail.Attributes.Add( "onBlur", "this.style.backgroundColor='white';" );

Avatar of udhayakumardudhayakumard🇮🇳

protected void Page_Load(object sender, EventArgs e)
    {
        TxtCompany.Attributes.Add("onfocus", "txtCompany_Enter()");
         txtCompany.Attributes.Add("onblur", "txtCompany_Leave()");
    }


    private void txtCompany_Enter()  
    {
        TxtCompany.BackColor = Color.Yellow;
        lblmsg.BackColor = Color.Yellow;
    }

        private void textBox1_Leave() // Back to normal
        {
            textBox1.BackColor = Color.Transparent;
            label1.BackColor = Color.Transparent;
        }

txtEmail.Attributes.Add( "onFocus", "this.style.backgroundColor='aliceblue';" );
okay this works


but when we call function  there is till error in the code
error is: object expected

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of udhayakumardudhayakumard🇮🇳

I dnt think there is a problem in this... May be u check someother part of the code. Else comment this and check...

txtEmail.Attributes.Add( "onFocus", "this.style.backgroundColor='aliceblue';" );

this part is working fine if we only need to change color of the text box and when we do not want to change color of the label
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.