Results 1 to 5 of 5

Thread: [RESOLVED] [02/03] Change the Textcolor in a RichTextbox.

  1. #1

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Resolved [RESOLVED] [02/03] Change the Textcolor in a RichTextbox.

    Hi All,

    What I'm trying to do is to change the Forecolor of each letter in a RichtextBox.

    I can already change the ForeColor but that's for all of the Text and not for each letter.

    This is what I've got:

    VB Code:
    1. If ColorDialog1.ShowDialog <> DialogResult.Cancel Then
    2.                     RichTextBox1.ForeColor = ColorDialog1.Color
    3.  
    4.                 End If

    Thanks in advance,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  2. #2
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [02/03] Change the Textcolor in a RichTextbox.

    Hi, you can't do it (to my knowledge) without going through selecting the letters you want to change the color of. This is how I do it:
    VB Code:
    1. RichTextBox1.SelectionColor = Color.Red
    Last edited by Half; Jul 15th, 2006 at 11:04 AM.
    VB 2005, Win Xp Pro sp2

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [02/03] Change the Textcolor in a RichTextbox.

    You can loop through the text and work with selection length?

    VB Code:
    1. this.richTextBox1.Text = "abcdefg";
    2.             this.richTextBox1.SelectionStart = 1;
    3.             this.richTextBox1.SelectionLength = 3;
    4.             this.richTextBox1.SelectionColor = System.Drawing.Color.Red;
    5.  
    6.             this.richTextBox1.SelectionStart = 4;
    7.             this.richTextBox1.SelectionLength = 2;
    8.             this.richTextBox1.SelectionColor = System.Drawing.Color.Blue;
    9.  
    10.             this.richTextBox1.SelectionLength = 0;

  4. #4

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [02/03] Change the Textcolor in a RichTextbox.

    Quote Originally Posted by Half
    Hi, you can't do it (to my knowledge) without going through selecting the letters you want to change the color of. This is how I do it:
    VB Code:
    1. RichTextBox1.SelectionColor = Color.Red
    Hi Half,

    That solved already my problem!

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  5. #5
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [RESOLVED] [02/03] Change the Textcolor in a RichTextbox.

    The selection thing is really fishy, if I was the user it'd look pretty lame to see some letters getting selected all the time I want to change the color. Also the selection should be unselected again so that the new color can be visible.

    If the user clicks anywhere in the richtextbox, then the whole selection logic in code goes to hell. Esp. if you want to color sequences after the first colored sequence. I always put the first selection's end in some var as a reference from where on to continue another coloring.

    I have experimented with setting the HideSelection to True, so the user doesn't notice the text to be colored go selected, then again unselected but this reloads the whole text when you reset HideSelection back to False.

    All this apply also if you want to underline (or make bold etc) only parts of the text.

    Another problem, if the user scrolls down and the text to be selected is above the visible text AND ends exactly at the end of a line, changing the color will scroll to the end of the selection thus not showing no colored text but the line after that. In this case you must move the caret to the start of the selection and then use the scrolltocaret method. Yet another variable to hold the selection start...

    Everyone would notice those things, I just thought to share my experience to make your work a bit easier
    VB 2005, Win Xp Pro sp2

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