Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one Label with Text : Project .....In this text i need pro in one color and ject in another color
Posted
Updated 26-Apr-18 2:02am
Comments
Shahin Khorshidnia 28-Feb-12 8:02am    
Please tag the question. WinForm? ASP.Net? WPF? ....?

You can only have one color in a label.
As an easy workaround, you can use two labels (with different font colors) to do this.

A more complex solution would be to build your own user control.
 
Share this answer
 
Comments
Ston Cold 28-Feb-12 6:50am    
+5 to Abhinav
Abhinav S 29-Feb-12 1:36am    
Thanks.
ajit_machhe 29-Feb-12 9:19am    
this is simplest solution..
Sanket Saxena 8-May-14 9:20am    
exactly...else it cant... +5 buddy
Once I had to create a "Gradient" Label... This is the code (works in Winforms)

public partial class GradientLabel : Label {

public LabelEx() {
    InitializeComponent();
}

protected override void OnPaint(PaintEventArgs e) {
    Font font = new Font("Tahoma", 48f, FontStyle.Bold);
    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, Width, Height + 5), Color.Gold, Color.Black, LinearGradientMode.Vertical);
    e.Graphics.DrawString(Text, font, brush, 0, 0);
}

}


This is not exactly "two color", but may lead you to a solution.
 
Share this answer
 
Comments
CHill60 8-May-14 18:22pm    
You *do* realise this question was over 2 years old? However good one.
A work-around. Create a bottom "button" with some of the text, then keep overlaying it with buttons containing some of the characters in different colors and transparent backgrounds, overlaying the bottom "button". I use "button" because all of them are dummy button-shaped objects. Only the top level item is an actual 'button', which really amounts to it having an onclick event.

This is practical enough, by the way, in that it is used in certain types of phishing scams - overlaying the real website data with transparent controls that accept the input, send it back to the scammers, and then either report an error (going to the real page for reentry) or even entering the text into the real screen as the invisible layer goes away.
 
Share this answer
 
Comments
Maciej Los 26-Apr-18 8:06am    
Dear W.Balboos,
This question is pretty old... (has been posted in 2012). Don't you think that OP is not interested in new answer?
W Balboos, GHB 26-Apr-18 8:11am    
Well - I pulled it from the 'current' list. Something brought it up. I noticed, now that you pointed it out, that answers are mainly 2012, another at 2014.

Perhaps solution 8 will satisfy whoever keeps activating it? For whatever reason, it was just updated. Maybe they're still working on this problem?
Jochen Arndt 26-Apr-18 8:19am    
Every time an answer is posted to an old question, it is brought back to the front of the list. While some might be useful, most are spam and some are wrong or misleading as in this case.

This was brought to the top of the list by two "Not an answer" which has been removed meanwhile. They were similar to solution 2 and 3 which do not recognised that the question is for a C# desktop application.

So allways check the date(s) before answering.
But you are not alone. It has happened to all of us.
W Balboos, GHB 26-Apr-18 8:24am    
Checking the dates - I can see how that makes sense. I'll probably end up doing it again because, alas, I often zone-in on things (quickly) and reading the question, existing answers, and determining if I've anything of value to add consume most of my mental threads.

None the less, I'll still try to keep check-the-date in mind, somewhere. Maybe when I read the "what I have tried" part.

Maybe a way to close out a question (after a year or so) to any further answers might be a solution?
Jochen Arndt 26-Apr-18 8:40am    
"Maybe a way to close out a question (after a year or so) to any further answers might be a solution?"

That has been already discussed in the Bugs&Suggestions forum but was rejected (AFAIR because answers might provide new sights - like yours).
try setting text property to html text like

lbl.Text="<span style="color:Red;">Pro</span><span><span style="color:Green;">ject</span>";
</span>


from back end of ASPX form.
 
Share this answer
 
Comments
itsureshuk 28-Feb-12 23:12pm    
Sorry Friend I need it in Windows Application
If you are using WPF it is pretty straightforward.
See my solution in Selective Text Coloring in WPF textBox[^]
You can do it with a Label in a comparable manner.
 
Share this answer
 
Comments
CHill60 8-May-14 18:22pm    
You *do* realise this question was over 2 years old?
Matt T Heffron 8-May-14 18:33pm    
:doh:
Nope, just saw that it (re)appeared in the recent QA list...
CHill60 8-May-14 18:37pm    
Yeah ... can't remember what revived it ... a site-driver I think. You're not the only one that got caught though ;-)
Gonzalo Brusella 9-May-14 15:37pm    
Just appeared as a "recent" question... (Didn't check the dates :'( )
I'd suggest you to use a RichTextBox, it is a standard component and can be used as a label by setting readonly to true and using HideCaret (see here: https://stackoverflow.com/questions/582312/how-to-hide-the-caret-in-a-richtextbox[^] for example)
 
Share this answer
 
Comments
CHill60 13-Mar-24 13:20pm    
The question is over 12 years old - however this is a perfectly valid answer that does bring something new to the thread so I am not going to downvote it.
However, it would have been better to have explained that from .NET 4.0 the IsReadOnlyCaretEnabled property of a RichTextBox is available and defaults to False so ReadOnly = true is sufficient.
Also adding how to set the text color of the RTB in your solution would have improved it even further.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900