Avatar of Mr_Fulano
Mr_FulanoFlag for United States of America

asked on 

Changing Font Color on a Label

Hi, I have a Windows Form in VB 2005, which contains several labels that provide the User with dynamic information at runtime. I would like to change part of the text when I apply my data to the label at runtime. For example, if the label provides the User with the name of a specific client, I would like to change the client's name to the color RED.

So, if my label is coded as shown below:

lblClientName.Text = "The name of the client is:  Sally Smith"

I would like to change 'Sally Smith' to the color RED. Not the entire label, just the name part.

How would I be able to do that programmatically?

Thanks,
Fulano
Visual Basic.NET

Avatar of undefined
Last Comment
Mr_Fulano
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

you can do this with a richtextbox
Avatar of lakshmidurga
lakshmidurga

if labelname=client1" then
labelname.styles.add("Color","red")
else
if labelname=client2" then
labelname.styles.add("Color","green")
end if
Avatar of Mr_Fulano
Mr_Fulano
Flag of United States of America image

ASKER

Clearly I must have mis-communicated my needs...

I need to change the color of part of a Label, not the entire label, and a RTB wouldn't apply, because I'm using a label not a textbox.
I believe this has something to do with Pens and Graphics, but I haven't really done much with colors programmatically, so need some help.

Ideally, I'd like to make my "text color changer" into a Function, so that I can simply say:

"The name of the client is: " changeToRED("Sally Smith")

and the results would be:
The name of the client is: Sally Smith    << Sally Smith in RED
Thanks,
Fulano
Avatar of Mr_Fulano
Mr_Fulano
Flag of United States of America image

ASKER

BTW, I just need the code to change the color, I can handle the Function part myself.
Fulano
you cannot use multiple colors in a single label, that's why I suggested to use a RTB instead to simulate a label (just give proper backcolor). To do this you would require to make the label owner-draw and draw the strings by yourself.
As an alternative you can use 2 labels with different colors each. Set both labels to be AutoSize. Once placed the text in both labels, you can move second label to be located at then end of first label.
ASKER CERTIFIED SOLUTION
Avatar of rachitkohli
rachitkohli
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Mr_Fulano
Mr_Fulano
Flag of United States of America image

ASKER

Hi Rachitkohli,

I think you're 99% of the way there...the only problem is that all the text is on top of each other instead of appending to the line.

Any thoughts,
Fulano
Avatar of rachitkohli
rachitkohli
Flag of India image

alright.. just clear the text from the label in design time, since we are setting text in the runtime
>>the only problem is that all the text is on top of each other instead of appending to the line.
Then why not to just use 2 separated label controls?
Avatar of Mr_Fulano
Mr_Fulano
Flag of United States of America image

ASKER

I thought of that, but when I do that all the text disappears. It seems that each string begins at index 0, so they all end up on top of each other.
Avatar of rachitkohli
rachitkohli
Flag of India image

Actually based on the code, it should not appear over each other, since thats the reason we are using the SizeF variable "sz" which takes care of the width of text in pixels. 0 is nothing but the Y-Coordinate which has to be same for all DrawString, since they all are going to be on a single line.
Are you using the code as it is or have done some changes,. Thats the same code which i wrote 20min bak & was working well. One thing, did I told you to set AutoSize Property of the label to False.

@jaime_olivares:
No need for two different labels, as this can be done over one label itself by handling the drawstring functions seperatly for change of fore color.
>>I thought of that, but when I do that all the text disappears. It seems that each string begins at index 0, so they all end up on top of each other
I cannot understand your concern, just create 2 controls, one on the top of the other. give them different colors.
ensure them don't overlap. change the .Text property of them when you desire.
Avatar of Mr_Fulano
Mr_Fulano
Flag of United States of America image

ASKER

Hi Rachitkohli, I guess I missed the part where you said:

>>One thing, did I told you to set AutoSize Property of the label to False.<<

After changing  that, it worked GREAT!!!  Very nice coding...

Thanks for the help,
Fulano
Avatar of rachitkohli
rachitkohli
Flag of India image

Am sorry to miss that property change, anyways glad to see you happy.. ;)
Avatar of Mr_Fulano
Mr_Fulano
Flag of United States of America image

ASKER

Jaime_olivares, with all due respect, allow me to share some thoughts.

In life we have choices. We can either do things the way their always done, or we can seek new challenges and learn something new. I don't question the use of two controls. However, I challenged the group to seek a way to thing outside the box. Rachitkohli did that and he did a great job.  Even if I never use this code again, I learned something new. My theory in life try to learn one new thing each day and it won't be a wasted day...

I appreciate your desire to help, but sometimes we might want to think things out a little and see if we can't build a better mousetrap.

Thanks for the help,
Fulano
Avatar of Mr_Fulano
Mr_Fulano
Flag of United States of America image

ASKER

Very innovative thinking...!!!
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo