Click to See Complete Forum and Search --> : Coloring a part of string(text ) in Stringbuilder
vcdebugger
September 21st, 2009, 01:34 AM
Hi,
I want to color just some part of text in a Stringbuilder class, any idea how to do it. that text will be drawn using DrawString function of graphics class...
FileData fileData = (FileData)this.Data;
_attributes = new StringBuilder("Variable(s)");
_attributes.Append("\n");
if (fileData.Variables.Count == 0)
{
_attributes.Append("[None]");
return;
}
for (int i = 0; i < fileData.Variables.Count; i++)
{
_attributes.Append(fileData.Variables[i].Name);
//Here I want to check some condition and color the text
_attributes.Append("\n");
}
thanks in advance
MNovy
September 21st, 2009, 01:38 AM
Well, a string is a string and nothing else.
A string does not care where it is printed later. So there is no way to "color" inside the
StringBuilder. You have to implement the coloration in your out print, like in a RichTextBox.
vcdebugger
September 21st, 2009, 01:47 AM
thanks for the reply. I thought there was some way to do it in C# because for me here i will displaying that text using co-ordianates and to calculate the co-ordinate of that particular text would be more cumbersome i thought...
anyways we can color it using :
g.DrawString
(
text,
Visuals.Core.LookAndFeel.Ambience.TextFontRegular,
Brushes.Black,
bounds,
Visuals.Core.LookAndFeel.Ambience.StringFormatML
);
here for me calculating the bounds each and every time would be cumbersome...
boudino
September 21st, 2009, 02:22 AM
Or you can make the string HTML and display it in browser control. You should be able to reach the same effect with RTF, but I think HTML is easier to use.
vcdebugger
September 21st, 2009, 03:46 AM
I am display this string on Form window and not in html page or RTF window..
boudino
September 21st, 2009, 06:37 AM
You can incorporate the browser control into the winform. Such a user control should be commonly available from Fx2.0
BigEd781
September 21st, 2009, 01:48 PM
here for me calculating the bounds each and every time would be cumbersome...
Why is it cumbersome? It should be simple enough to create a method that will calculate the bounds each time you need to.
JonnyPoet
September 21st, 2009, 02:40 PM
Coloring a string if you are userdrawing it can be done in any way you want to do it. You only need to create any system where your later during drawing the string could decide whats the string and whats the commands or atributes.
All together id built up in one string like "Here is [Color=red ] my Text [/ red] "
You can create your attributes in your own way or using already known standards like HTML or RTF
If you are consequent your own idea per sure also would work, but if there aleady exists standards, whats the reason not using them. Re- creating the wheel again? Any real reason for that?
Any compatibility to known systems will need lots of conversion while using already known patterns are easy to use and much easier to maintain.
You can use for exmple HTML coding but still parsing that code in your own graphics method and getting the result this way directly drawn to the form.
Having attributes in the end of the text has lots of disadvantages. Doing that your attibutes needs to reference to the point where each of your attributes begins and ends. For example think you simple add one sign in the middle of the text, all your references to text after that change needs to be corrected. XML and using tags for your Attributes is maybe a better solution. But I personally would simple use HTML similar to what we use here Look how the code in the editor is colored and you have an easy solution.
darwen
September 21st, 2009, 05:29 PM
If you want advanced graphics like this, I'd go down the WPF (Windows Presentation Foundation) route.
It has much better built-in graphics support than WinForms.
Darwen.
vcdebugger
September 22nd, 2009, 06:32 AM
You can incorporate the browser control into the winform. Such a user control should be commonly available from Fx2.0
sorry I am not getting what you are telling. In my case I dont want any browser/html or anything of that sort. I am using graphics library to draw plane shapes and text eg: class diagrams...
vcdebugger
September 22nd, 2009, 06:35 AM
Why is it cumbersome? It should be simple enough to create a method that will calculate the bounds each time you need to.
cumbersome in the sense i have to split the text in to individual components get the string size and then calculate the X and Y coordinates for and display it properly with my rectangular BOUND within proper height and width and it should not come out of the boundary.
thats why thought is there is some mechanism to color the TEXT within in string or string builder..
vcdebugger
September 22nd, 2009, 07:01 AM
Coloring a string if you are userdrawing it can be done in any way you want to do it. You only need to create any system where your later during drawing the string could decide whats the string and whats the commands or atributes.
All together id built up in one string like "Here is [Color=red ] my Text [/ red] "
You can create your attributes in your own way or using already known standards like HTML or RTF
If you are consequent your own idea per sure also would work, but if there aleady exists standards, whats the reason not using them. Re- creating the wheel again? Any real reason for that?
Any compatibility to known systems will need lots of conversion while using already known patterns are easy to use and much easier to maintain.
You can use for exmple HTML coding but still parsing that code in your own graphics method and getting the result this way directly drawn to the form.
Having attributes in the end of the text has lots of disadvantages. Doing that your attibutes needs to reference to the point where each of your attributes begins and ends. For example think you simple add one sign in the middle of the text, all your references to text after that change needs to be corrected. XML and using tags for your Attributes is maybe a better solution. But I personally would simple use HTML similar to what we use here Look how the code in the editor is colored and you have an easy solution.
sorry , I think what you are talking is no way related to my problem here. It is about coloring of text while display it on form window using grpahics class.
html/xml does not come in to picture here at all !!. anyways thanks for the reply :)
vcdebugger
September 22nd, 2009, 07:15 AM
If you want advanced graphics like this, I'd go down the WPF (Windows Presentation Foundation) route.
It has much better built-in graphics support than WinForms.
Darwen.
thanks,... I will try to study about WPF. :)
JonnyPoet
September 22nd, 2009, 02:20 PM
Well, a string is a string and nothing else.
A string does not care where it is printed later. So there is no way to "color" inside the
StringBuilder. You have to implement the coloration in your out print, like in a RichTextBox.Yes thats what I'm also talking about.
If you are ownerdrawing on a form you are able to use tags for colorand size of text and all that. The textcolor simple has to do with the brush you are using for drawing it. So if your text is in a way of RTF formatted or HTML formatted your code in the drawing method can parse that text and coloring sizing whatever you need.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.