Click here to Skip to main content
15,903,201 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
is it possible to assign a color for an variable value using C# windows application.

for an example., int i = 5;

place the answer in toolstripbutton. like "Notification(5)". I only want to change the color for '5'. Not for all. I
s it possible, can anyone help me with this...
Posted
Updated 26-Dec-15 2:54am
v2
Comments
BillWoodruff 26-Dec-15 9:01am    
Please describe more clearly what you are doing here, and what you want. Of course, you can define a variable of Type 'Color:

public System.Drawing.Color Color5 = Color.AliceBlue;

You can,
But only problem is toolstripbutton works as a single control.
If you want to change the text displaying 'Notification (5)' where 5 is suppose red in color, you have to find a way to differentiate the text,
Since, the text is one word itself if color of toolstripbutton is changed, so does the whole text,
You have to work around with two texts for this.
 
Share this answer
 
There is no standard way to do this: you would have to handle the Paint event for the button yourself and draw the text onto it in multiple colours - which isn't as trivial as it sounds, because you need to use MeasureString three times to get each part size ("Notifications(" is one, "5" is the second, and ")" is the third) then centre all of the text when you have it's "composite" length).

I'd probably cheat, and create a ten or so images: 1-9 and "++" for more than ten instead of faffing with text on my button.
 
Share this answer
 
Comments
MSVelu8788 26-Dec-15 10:41am    
Thanks for the quick reply griff., Can you share me some example codes
OriginalGriff 26-Dec-15 10:45am    
:laugh:
No - I've never wanted to draw multicoloured text on a button! ;)
But if you look at the MSDN MeasureString documentation:
https://msdn.microsoft.com/en-us/library/6xe5hazb(v=vs.110).aspx
it includes a basic example of how to use it. You should be able to work it out from there.

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