Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » how to change WinForm controls' text color programatically?
Closed Thread
Old 03-04-2009, 06:08 PM   #1 (permalink)
 
Junior Techie

Join Date: Nov 2008

Posts: 79

BobLewiston is on a distinguished road

Default how to change WinForm controls' text color programatically?

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.
BobLewiston is offline  
Old 03-04-2009, 06:19 PM   #2 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: how to change WinForm controls' text color programatically?

When you are setting a property on any object, you need to pay attention to the property's type. For instance, a Label has a property called "ForeColor" of type System.Drawing.Color. Thus, to specify the label's text color, you need to choose one of the colors in the System.Drawing.Color structure.
jaeusm is offline  
Old 03-04-2009, 07:06 PM   #3 (permalink)
 
Junior Techie

Join Date: Nov 2008

Posts: 79

BobLewiston is on a distinguished road

Default Re: how to change WinForm controls' text color programatically?

attention, jaeusm:

Yes, that answers my question about how to get the compiler to recognize the color I want: by saying
Code:
this.ForeColor = System.Drawing.Color.Red
instead of
Code:
this.ForeColor = Red;
but it doesn't change the color of the text already displayed, even when I reassign the string displayed to the text field of the control. Can you tell me how to make the control redisplay the text in the new color?

BobLewiston is offline  
Old 03-05-2009, 12:48 PM   #4 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: how to change WinForm controls' text color programatically?

There is no "text field" control that I'm aware of, so I'll have to assume you mean TextBox. If you want to set the ForeColor on a TextBox, then you need to simply set the ForeColor property on the TextBox object in question. The code you posted above is setting the color on the form.
jaeusm is offline  
Old 03-05-2009, 01:48 PM   #5 (permalink)
 
Junior Techie

Join Date: Nov 2008

Posts: 79

BobLewiston is on a distinguished road

Default Re: how to change WinForm controls' text color programatically?

jaeusm:

Sorry, what I meant was:
Code:
using System; 
using System.Windows.Forms; 
  
namespace ChangeTextColor
{ 
    public partial class TestForm : Form
    { 
        public TestForm () 
        { 
            InitializeComponent (); 
            textBox1.Text = "This is the original text."; 
        } 
  
        private void btn_Click (object sender, EventArgs e) 
        { 
            textBox1.Text = "Now the text is red."; 
            textBox1.ForeColor = System.Drawing.Color.Red; 
        } 
    } 
}
The "text field of the control" I was referring to is textBox1.Text.

Anyway, 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.
BobLewiston is offline  
Old 03-05-2009, 03:47 PM   #6 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: how to change WinForm controls' text color programatically?

Quote:
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;
Actually, the code you posted works fine for me without setting the BackColor property.
jaeusm is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
email text color change pcman63 Windows Operating Systems and Software 2 01-26-2009 09:20 AM
Computer Still acting screwy Shana2415 Virus - Spyware Protection / Detection 10 09-03-2008 11:36 AM
Text encryption tools White_Wolf Off Topic Discussion 4 01-13-2008 09:34 AM
Send Text Messages From your PC. KCRMYZ NEW Technology Services & Products 0 08-16-2007 05:40 PM