CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Coloring a part of string(text ) in Stringbuilder

    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...

    Code:
     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

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Coloring a part of string(text ) in Stringbuilder

    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.

  3. #3
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Coloring a part of string(text ) in Stringbuilder

    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 :

    Code:
    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...

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Coloring a part of string(text ) in Stringbuilder

    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.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  5. #5
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Coloring a part of string(text ) in Stringbuilder

    I am display this string on Form window and not in html page or RTF window..

  6. #6
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Coloring a part of string(text ) in Stringbuilder

    You can incorporate the browser control into the winform. Such a user control should be commonly available from Fx2.0
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Coloring a part of string(text ) in Stringbuilder

    Quote Originally Posted by vcdebugger View Post
    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.

  8. #8
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Coloring a part of string(text ) in Stringbuilder

    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.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  9. #9
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Coloring a part of string(text ) in Stringbuilder

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  10. #10
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Coloring a part of string(text ) in Stringbuilder

    Quote Originally Posted by boudino View Post
    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...

  11. #11
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Coloring a part of string(text ) in Stringbuilder

    Quote Originally Posted by BigEd781 View Post
    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..

  12. #12
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Coloring a part of string(text ) in Stringbuilder

    Quote Originally Posted by JonnyPoet View Post
    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

  13. #13
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Coloring a part of string(text ) in Stringbuilder

    Quote Originally Posted by darwen View Post
    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.

  14. #14
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Coloring a part of string(text ) in Stringbuilder

    Quote Originally Posted by MNovy View Post
    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.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured