473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.
Mar 4 '09 #1
13 18122
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1.             btnTest.ForeColor = Color.Red;
  2.  
Mar 4 '09 #2
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
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. btnTest.Invalidate();
Should cause it to repaint.
Mar 5 '09 #4
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
3,525 Expert 2GB
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
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
3,525 Expert 2GB
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
3,525 Expert 2GB
.... 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
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
7,872 Expert 4TB
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
Plater:

Have you tried this without assigning a color to BackColor? (See my previous message.)
Mar 5 '09 #12
Plater
7,872 Expert 4TB
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
3,525 Expert 2GB
@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

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

Similar topics

1
by: Soumya Roy Choudhury via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Soumya Roy Choudhury I have a datagrid with five columns.I want to change the text color of all cell in second and fourth...
1
by: Soumya Roy Choudhury via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Soumya Roy Choudhury I have five column in a datagrid.I want to change the text color of all cell in first and fourth column.How...
2
by: Dave | last post by:
I need to change the radiobuttonlist text color at page load. How do I do this?
0
by: Tim | last post by:
Hi there You have seen a progress bar, the kind with the number 100% (in blue text) written in the middle. When the progress reaches where the 100% is written the color of the pecentage changes from...
2
by: Amy | last post by:
<style> div.helpBtn{ font:bold 73% verdana; color:#995F8D; text-align:left; width:79px; height:24px; margin:0px; padding: 5px 0px 0px 27px; background:...
1
by: jcrouse | last post by:
I am looping throught the items in a checked list box. If it helps, all of the items are checked. If I string of text matches a certain criteria I would like to change it's text color to red. I...
0
by: musawer | last post by:
Can Someone give a good link for C# Winform DataGrid Text Style Formating (Font,Color,Size etc)... Quick reply will be highly appreciated... Thanks...
2
by: Peted | last post by:
Are there any options to enable an antialias function on standard winform controls ? things like the displayed text on buttons , radio buttons and other standard controls look a bit jagged and...
1
by: dhaneshrs | last post by:
I have a small code that shows inactive and active users from the ms access DB. <%@ Page Language="VB" MasterPageFile="~/MasterPageAdmin.master" Title="Welcome" %> <%@ Import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.