By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

how to change WinForm controls' text color programatically?

P: 93
Can anybody tell me how to change WinForm controls' text color programatically? I have no problem finding the fields to assign colors to, but the compiler doesn't recognize any color I mention, whether it's something like WindowText, or something like Red.

BTW, I'm using VC# Express 2008.
Mar 4 '09 #1
Share this Question
Share on Google+
13 Replies


tlhintoq
Expert 2.5K+
P: 3,525
Expand|Select|Wrap|Line Numbers
  1.             btnTest.ForeColor = Color.Red;
  2.  
Mar 4 '09 #2

P: 93
attention, tlhintoq:

Thanks, but then how do you get the control text to redisplay so you can see the change you've made to the text color? I've tried changing the text color as you recommended, and I've tried that plus reassigning the string that goes in the text field, but that isn't working.
Mar 4 '09 #3

tlhintoq
Expert 2.5K+
P: 3,525
Expand|Select|Wrap|Line Numbers
  1. btnTest.Invalidate();
Should cause it to repaint.
Mar 5 '09 #4

P: 93
No, it's not working, although conceivably the problem is that repainting the text in a textbox (which is what I'm trying to do) is somehow different than repainting a button.
Mar 5 '09 #5

tlhintoq
Expert 2.5K+
P: 3,525
I don't know why it isn't working for you. Maybe you could share the code you are using. This is working for me.

Expand|Select|Wrap|Line Numbers
  1.         private void textBox1_TextChanged(object sender, EventArgs e)
  2.         {
  3.             if (textBox1.Text == "Pink") textBox1.ForeColor = Color.Pink;
  4.             if (textBox1.Text == "Red") textBox1.ForeColor = Color.Red;
  5.             if (textBox1.Text == "Blue") textBox1.ForeColor = Color.Blue;
  6.             textBox1.Invalidate();
  7.         }
  8.  
If you type the word "Pink" in the text box the text will change color to pink, and so on.
Mar 5 '09 #6

P: 93
Here's what I discovered through trial and error: it turns out that when a TextBox's ReadOnly property is set to true (as this one was), the Text property can be modified, but the ForeColor property can't. I even tried it in two different programs. It has been suggested that this sounds like a bug in .Net. I wonder if it's just a problem with my particular installation of VC# Express 2008.

Anyway, here's the form1.cs for a little test program that produces the error, at least on my computer. It's just a WinForm with one button and one TextBox.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace ChangeTextColor2
  5. {
  6.     public partial class TestForm : Form
  7.     {
  8.         public TestForm ()
  9.         {
  10.             InitializeComponent ();
  11.             textBox1.Text = "This is the original text.";
  12.         }
  13.  
  14.         private void btn_Click (object sender, EventArgs e)
  15.         {
  16.             textBox1.Text = "Now the text should be turned red.";
  17.             textBox1.ForeColor = System.Drawing.Color.Red;
  18.         }
  19.     }
  20. }
Mar 5 '09 #7

tlhintoq
Expert 2.5K+
P: 3,525
Ohhhh.... This is a read-only text box. [New information here]

I wouldn't call that a bug. If a textbox is read only there are specific UI rules to follow. Same as if it enabled=false.

May I make a suggestion? If the textbox is read-only then you aren't expecting user input. You're just using it for display of information purposes, right? Use a label instead. A label is 'read-only' and you can make it any text color you like.
Mar 5 '09 #8

tlhintoq
Expert 2.5K+
P: 3,525
.... Or.... Make the box NOT read-only, but handle the KeyPress events for it so that no input ever makes it to the control. Now you have changeable color but the user can't do anything to the text.
Mar 5 '09 #9

P: 93
As I will no doubt say many times, please bear with me, since I'm a newbie.

How do I handle the KeyPress events? If I double-click on the TextBox, I'll find myself ready to write code into method textBox1_Click, but how do I get into a method called textBox1_KeyPress? (Nor do I see any property KeyPress for a TextBox.)

P.S. The label idea worked fine. Thanks.

P.P.S. It turns out that changing (or just reassigning an unchanged value to) the TextBox's BackColor property when also changing the ForeColor property somehow magically makes it work; don't ask me why.
Mar 5 '09 #10

Plater
Expert 5K+
P: 7,872
my readonly textboxes change colors just fine.
Expand|Select|Wrap|Line Numbers
  1.             tbSearch.ReadOnly = true;
  2.             tbSearch.ForeColor = Color.Red;
  3.             tbSearch.BackColor = Color.Brown;
  4.             tbSearch.Text = "this is some test text";
  5.  
my textbox has brown background and red text. I didn't even need to call invalidate.
Mar 5 '09 #11

P: 93
Plater:

Have you tried this without assigning a color to BackColor? (See my previous message.)
Mar 5 '09 #12

Plater
Expert 5K+
P: 7,872
Oops I guess I came late to the party. I started typing the message then went to lunch and forgot.
I don't know why the backcolor issue is the way it is, but its the same on a webpage in IE6 as well,
Mar 5 '09 #13

tlhintoq
Expert 2.5K+
P: 3,525
@BobLewiston
Please don't think me rude when I make this suggestion.
Take some time to learn your way around Visual Studio, before you worry about writing a lot of code. Knowing the tools available to you will make your life so much easier. If the only tool you know about is a hammer, you work hard to make every job work for a hammer. Visual Studio has a great many features, most of which to make your life easier.

Keyboard shortcuts like ctrl-M, ctrl-O to toggle all the methods to definitions are a great help.

Knowing where to find all the events also shows you what events are available to you "out of the box" which can spawn new ideas of how or what you can do.



I HIGHLY recommend this book: It does a good job introducing you to Visual Studio, C#, the steps of making a program including debugging tools available to you, touches on WPF and database integration as well as internet service. It walks you through building two programs: A used car lot inventory application and making your own web browser. If you're not careful, you just might learn something along the way.
Mar 6 '09 #14

Post your reply

Sign in to post your reply or Sign up for a free account.