String.Format with different color

Question

Thursday, December 27, 2012 9:12 AM

Hi all,

I want to show a string with different font color. 

Sample: I love you. With "love" is a parameter. => String.Format("I {0} you"). I want change color for string is input parameter.

Please for me support above this. 

Thanks all.

All replies (6)

Friday, December 28, 2012 5:29 PM âś…Answered

You can use a RichTextBox control to colorize string and show result to user. If you need to know how to work, please read Rich Text Format and below articles:

RichTextBox

Writing colorized text to RichTextBox

Extended RichTextBox

Any fool can know. The point is to understand.(Albert Einstein)


Thursday, December 27, 2012 9:23 AM | 1 vote

Hi,

a string is just a number of characters. They do not have any size or color. So I fear that this is not possible.

You can have a string that contains a coding. A well known coding is HTML. So in HTML you can add stylings to the characters.

Maybe you can give more details about your requirements / your project? It seems that you might need to modify the geenral solution because the string class simply does not contain what you need.

With kind regards,

Konrad


Thursday, December 27, 2012 9:32 AM

Formatting is not the correct place to decide the color of the string. You should handle string colors where the string is drawn or printed to the device.


Thursday, December 27, 2012 10:19 AM

Hi,

a string is just a number of characters. They do not have any size or color. So I fear that this is not possible.

You can have a string that contains a coding. A well known coding is HTML. So in HTML you can add stylings to the characters.

Maybe you can give more details about your requirements / your project? It seems that you might need to modify the geenral solution because the string class simply does not contain what you need.

With kind regards,

Konrad

Thanks for your answer. I make a application for datamining project. Now i want to show a rule in a control and make sure users can understand it. I try to make a tooltip. Sample: If customer buy "Sugar" may be he buy "Milk" with confident is x %. I try to change color of input parameter for users ease to undertand it. If you know datamining, Please for me method to show rules for explain: X -> Y with confident is xx%


Thursday, December 27, 2012 10:21 AM

Formatting is not the correct place to decide the color of the string. You should handle string colors where the string is drawn or printed to the device.

I make a application with c# language, Please for me method. It's a string. and i want to show it to tooltipcontroller.


Thursday, December 27, 2012 10:36 AM

Hi,

so first of all, I would not store all together in a string. You called it Rule (maybe you find a better name for it? It is more or less a statistic thing or not?) so you could create a class Rule. It has something like a BoughtProduct and then maybe a list of pairs of AdditionalProduct and a confident number.

That is the core data and you should have it as core data in your application. That is the core business that you gave us so far. So this stays inside the business logic.

Now you have different clients. So you might have a WPF part in which you have a control which can display such Rules. And in there you can build everything and use colors as you want. Or if your own control is to much, you could think about any other solution e.g. you could put something like a RichTextBox on your application and you have some code that writes everything in there - including colors, fonts, sizes, ...

And you might have somehting like that for ASP.Net, too. And maybe a Windows Forms solution ...

So a common practice is to seperate business logic from any display logic. The Rule does not know anything about how it is displayed. And it does not care about it. And it is not interested in that at all. So you can even have clients that do not have any color (e.g. for blind people using braile displays or so).

With kind regards,

Konrad