Forum guidelines
AuthorQuestion
Change the foreColor of a textbox
Posted By Craig Kasold on 11 Feb 2009
I am trying to do something that I believe should be very simple.  I am trying to change the forecolor of a textbox depending on what is in the text box.  Meaning if the text box has a value of 'UP', I want the forecolor to be RED.  If the text box has a value of 'DOWN', I want the forecolor to be BLUE.  I am use to programming in SQL (Oracle) over 10 years ago and I would simply state something like: if the textbox.text  = "UP" then textbox.forecolor = color.red; ELSIF textbox.text = "DOWN" then textbox.forecolor = color.blue; ELSE textbox.forecolor = color.black;

How does one do it in Visual C#?
Can anyone Help?


Excited about learning ASP.NET
AuthorReply
Re: Change the foreColor of a textbox
Posted By John Penn on 11 Feb 2009  
That is how you do it... set the ForeColor property of the TextBox control.

Re: Change the foreColor of a textbox
Posted By Craig Kasold on 11 Feb 2009  
I thought I replied to this, but did not see it post, so I'll try again.

John,
Thank you for your quick replay; however, that does not work, Hence the question.  When I try it as follows: If textbox.text = "UP" I get "cannot implicitly convert type 'string' to 'bool'.  When I put a == I get that only certain expressions can be used as a statement. 

I have it where the text box is populated when I enter the menustrip (Navigation menu) and that works great.  I even have the text box changing colors when I enter the textbox.  This is just something that would be kind of neat to do - changing the color based on the value of the text.  I can imagine that things would neat to change depending on certain values entered by the user.

I know that this can not be THAT difficult, I just don't know how to do it yet.

Can anyone help?  Or Will anyone help me?

Excited about learning ASP.NET
John Penn
  • 0
  • 0
accepted
Re: Change the foreColor of a textbox
Posted By John Penn on 11 Feb 2009   Accepted Answer
Here is a sample, assuming 3 things here:

1)  The textbox control is named "textBox1"
2)  You want the color to change when textBox1 loses focus
3)  Your comparison of the textbox text is case-sensitive

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text.Equals("DOWN"))
                textBox1.ForeColor = Color.Blue;
            else
                if (textBox1.Text.Equals("UP"))
                    textBox1.ForeColor = Color.Red;
                else
                    textBox1.ForeColor = Color.Black;
        }


Re: Change the foreColor of a textbox
Posted By Craig Kasold on 11 Feb 2009  
John,

Thank you - I see that what I needed were a few of the '(' and ')'.  Thank you.
I knew it was simple.  I can work with that and expand it.

Thank you again.

Have a Great Day.

Craig

Excited about learning ASP.NET

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.