Click here to Skip to main content
15,849,678 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 3: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).
lbl.Text = "<span style="color:Red;">TextPart1</span><span style="color:White;">" + textvariable + "</span>";

Its really working I tested
 
Share this answer
 
v4
Comments
CHill60 8-May-14 18:23pm    
You *do* realise this question was over 2 years old? And Winforms?
Suresh Bavireddy 9-May-14 6:47am    
Of course ho wold it may be a question is always a question.. yesterday I'm in same need..Of course it is 2 years old but I found it helpful and it worked for me. After sometime someone might look for the same... Whats wrong in posting an answer.
CHill60 9-May-14 7:19am    
Nothing wrong in posting an answer if it adds something that hasn't already been mentioned ... in this case the poster posted a virtually identical answer as Solution 2 and ignored the OP comment "Sorry Friend I need it in Windows Application"
Member 11021961 12-Oct-15 8:52am    
champ... you tested it... first test n then say it works.

:O
Member 11021961 12-Oct-15 8:52am    
in window application
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 :'( )

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