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.
Loading

Aleksandar IlioskiRank #9,295 · 208 reputationPosted Mar 4, 2009, 1:32 PM EST
Aleksandar IlioskiRank #9,295 · 208 reputationPosted Mar 4, 2009, 1:38 PM EST
this
.ForeColor = System.Drawing.Color.Red;//is this what you are lookng for?
Bob Lewiston77 reputationPosted Mar 4, 2009, 2:00 PM EST
attention, Aleksander Ilioski:
Yes, I think that answers my question about how to get the compiler to recognize the color I want: by saying
this.ForeColor = System.Drawing.Color.Red
instead of
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?
Aleksandar IlioskiRank #9,295 · 208 reputationPosted Mar 4, 2009, 2:23 PM EST
put "this.ForeColor = System.Drawing.Color.Red;" before the assinging the string
I can't help you like this, give me some code...(some example, you don't have the same code if you don't want to)
Bob Lewiston77 reputationPosted Mar 5, 2009, 6:25 AM EST
attention, Aleksandar Ilioski:
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.
using System;
using System.Windows.Forms;
namespace ChangeTextColor2
{
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 should be turned red.";
textBox1.ForeColor = System.Drawing.Color.Red;
}
}
}
Aleksandar IlioskiRank #9,295 · 208 reputationPosted Mar 5, 2009, 9:01 AM EST
Hi again
You're rignt, if the textBox is readOnly it does't chage the foreColor. I have 2 solutions for you :)
class
mojTextBox:System.Windows.Forms.TextBox{
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e){
SolidBrush drawBrush = new SolidBrush(ForeColor); //Use the ForeColor property //Draw string to screen.e.Graphics.DrawString(Text, Font, drawBrush, 0f, 0f);
//Use the Font property}
public mojTextBox(){
this.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, true); //InitializeComponent();}
}
here you can read something about it: http://www.syncfusion.com/FAQ/windowsforms/faq_c94c.aspx#q866q
Hope this will solve your problem.
I think that you should use label instead of textBox