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.
13 18122 - btnTest.ForeColor = Color.Red;
-
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.
Should cause it to repaint.
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.
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. -
private void textBox1_TextChanged(object sender, EventArgs e)
-
{
-
if (textBox1.Text == "Pink") textBox1.ForeColor = Color.Pink;
-
if (textBox1.Text == "Red") textBox1.ForeColor = Color.Red;
-
if (textBox1.Text == "Blue") textBox1.ForeColor = Color.Blue;
-
textBox1.Invalidate();
-
}
-
If you type the word "Pink" in the text box the text will change color to pink, and so on.
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;
-
}
-
}
-
}
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.
.... 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.
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.
my readonly textboxes change colors just fine. -
tbSearch.ReadOnly = true;
-
tbSearch.ForeColor = Color.Red;
-
tbSearch.BackColor = Color.Brown;
-
tbSearch.Text = "this is some test text";
-
my textbox has brown background and red text. I didn't even need to call invalidate.
Plater:
Have you tried this without assigning a color to BackColor? (See my previous message.)
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,
@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. Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
by: Dave |
last post by:
I need to change the radiobuttonlist text color at page load. How do I do this?
|
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...
|
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:...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
| |