Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows Form Change Color of Active TextBox

Status
Not open for further replies.

RJL1

Technical User
Oct 3, 2002
228
US
I'm learning C# and I'm working on a windows form with 12 or so fields and would like to change the background color of the text box when it becomes the active text box. Any help with this is appreciated

I do not have any code as a starting point

Thanks
RJL
 
Stop. Don't go there. Depending on where you live and who uses your software, you could be in violation of the law. The user should choose their own colors in Windows Display settings and you should honor their choices.

Craig Berntson
MCSD, Visual C# MVP,
 
easy craigber... it's an innocent question. and we don't have a full requirements list, so we cannot say it's against the law.

now to change the color on the current control, look for an event on the textbox named Focus or Activate, or something like that. change the background color to something else, when the control looses focus (Blur?) change the color back.
if might look somehthing like this
Code:
class myform : form
{
   public myform()
   {
      thetextbox.Focus += ToggleHighlight;
      thetextbox.Blur += ToggleHighlight;
   }

   public override void Dispose(bool disposing)
   {
      thetextbox.Focus -= ToggleHighlight;
      thetextbox.Blur -= ToggleHighlight;

      base.Dispose(disposing);
   }

   private void ToggleHighlight(object sender, EventArgs e)
   {
      var control = (textbox)sender;
      var currnet = control.BackgroundColor;
      control.BackgroundColor = current == Color.Pink ? Color.White : Color.Pink;
   }
}

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks for the feedback. I was not aware this would be a legal issue. The form is for internal use in a very nich enviroment by maybe 2 people. The seever where this resides is locked down so usrs cannot make changes to nay settings that would affect colors anyways.

This was not a requirement but more for learning how to.

Thanks again
RJL
 
I never said it WAS against the law, just that it may be. It's surprising how many people don't have any understanding of how laws affect software. In the US, the two big ones are the Americans With Disabilities Act (ADA) and Sarbanes-Oxley (SOx).

But, they only kick in under specific conditions, primarily if you are a big company and/or producing software for internal use and/or a government contractor and/or the government buys your software.

However, they are very serious about it. I know of a company that their salesman was prohibited from using their PowerPoint deck when make a sales presentation to the Feds because the slide colors chosen were in violation of the ADA.

Craig Berntson
MCSD, Visual C# MVP,
 
Oh.. I almost forgot. The colors chosen may not actually be seen or cause the text to be hidden due to color blindness, which is more common that people think.

Craig Berntson
MCSD, Visual C# MVP,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top