locked
How to change text color in VB.net RRS feed

  • Question

  • Hi all, 
    I am inserting COMMENTS and COMMAND from text box to rich text box,  
    I want to change color of Comments to green and Commands to red and write it in rich text box. 
    I am able to write the normal text (that is in black color) in a file. But I am not able to convert the text color. 

    Could some body please help with the piece of code. 

     thanks,

    Shijirunara

    Wednesday, December 8, 2010 8:02 AM

Answers

  • Hello,

    first: You can change colors in a Richtextbox by doing a .SelectionStart, .SelectionLenght, .SelectionBackColor, .Selection Color.

    The following Code demonstates how to change Colors in a Text, insert a Text at the end of a specified line.

    I think, this may be helpfull for You.

    best regards Ellen

     

     

    Private Sub modify(ByVal rb As RichTextBox, _
          ByVal line As Integer, _
          ByVal msg As String)
    
      Static pos1 As Integer
      Static lineOld As Integer
      Dim posn As Integer
      'color letzter selection wieder aufheben
      rb.SelectionStart = pos1
      rb.SelectionLength = rb.Lines(lineOld).Length
      rb.SelectionBackColor = Color.White
    
      pos1 = rb.GetFirstCharIndexFromLine(line)
      rb.SelectionStart = pos1
      rb.SelectionLength = rb.Lines(line).Length
      rb.SelectionBackColor = Color.Coral
      rb.ScrollToCaret()
    
      'selection am Ende der zeile setzen:
      posn = pos1 + rb.Lines(line).Length
      rb.SelectionStart = posn
      rb.SelectionLength = 0
      Clipboard.SetText(msg)
      rb.Paste()
      lineOld = line
    
     End Sub
    
    

    Ich benutze/ I'm using VB2008 & VB2010
    • Proposed as answer by John Anthony Oliver Wednesday, December 8, 2010 4:30 PM
    • Marked as answer by Kee Poppy Wednesday, December 15, 2010 2:01 AM
    Wednesday, December 8, 2010 8:57 AM

All replies

  • You can easily format the content of a rich text box by font property.


    richTextBox.SelectionFont = <myFont>;
    richTextBox.SelectionColor = Color.<smyColor>;

    this would change the font color of the selected text.

    Is this what you are looking for?

    Wednesday, December 8, 2010 8:52 AM
  • Hello,

    first: You can change colors in a Richtextbox by doing a .SelectionStart, .SelectionLenght, .SelectionBackColor, .Selection Color.

    The following Code demonstates how to change Colors in a Text, insert a Text at the end of a specified line.

    I think, this may be helpfull for You.

    best regards Ellen

     

     

    Private Sub modify(ByVal rb As RichTextBox, _
          ByVal line As Integer, _
          ByVal msg As String)
    
      Static pos1 As Integer
      Static lineOld As Integer
      Dim posn As Integer
      'color letzter selection wieder aufheben
      rb.SelectionStart = pos1
      rb.SelectionLength = rb.Lines(lineOld).Length
      rb.SelectionBackColor = Color.White
    
      pos1 = rb.GetFirstCharIndexFromLine(line)
      rb.SelectionStart = pos1
      rb.SelectionLength = rb.Lines(line).Length
      rb.SelectionBackColor = Color.Coral
      rb.ScrollToCaret()
    
      'selection am Ende der zeile setzen:
      posn = pos1 + rb.Lines(line).Length
      rb.SelectionStart = posn
      rb.SelectionLength = 0
      Clipboard.SetText(msg)
      rb.Paste()
      lineOld = line
    
     End Sub
    
    

    Ich benutze/ I'm using VB2008 & VB2010
    • Proposed as answer by John Anthony Oliver Wednesday, December 8, 2010 4:30 PM
    • Marked as answer by Kee Poppy Wednesday, December 15, 2010 2:01 AM
    Wednesday, December 8, 2010 8:57 AM
  • You can easily format the content of a rich text box by font property.


    richTextBox.SelectionFont = <myFont>;
    richTextBox.SelectionColor = Color.<smyColor>;

    this would change the font color of the selected text.

    Is this what you are looking for?


    Hi vijaykamat,

    Please remember this is a VB.Net forum.

    Semi-colons are not used at the end of lines in VB.Net. Otherwise your answer is valid.



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Wednesday, December 8, 2010 4:28 PM
  • Hi Shijirunara,

    Are your talking about VB.Net style comments starting with a single apostrophe? This one >>  '

    Do you want code to also detect comments from other areas of development such as C, C#, C++ and HTML comments?

    You could do this a number of ways, one would be to insert a COMMENT if you left-click on a specific TextBox and with a right-click insert COMMAND text, or you could use two separate buttons.

    Alternatively you could have code to sort the entire coloring for you, all in one go depending on what the line starts with or whatever.



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Wednesday, December 8, 2010 4:36 PM
  • Oh sorry John, I was careless..

    Thank you for understanding :)

    Thursday, December 9, 2010 3:28 AM