Results 1 to 8 of 8

Thread: [Resolved] [2005] Adding color to text in a RichTextBox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2009
    Posts
    31

    [Resolved] [2005] Adding color to text in a RichTextBox

    Hi,

    I am opening an exe that launches in a hidden cmd window, and redirecting the text to a RichTextBox on a form.

    The problem is that the status messages in the cmd window are in color, but the redirected text in the RichTextBox has no color information and show as black on white.

    However, all the status messages in the cmd window have headers that could be parsed in some way so I can add color to them for my RichTextbox.

    For example, a message in the cmd window might read:

    [Warning] No settings file was found, using default values instead.

    This message is in Yellow in the cmd window.

    The text gets into my RichTextBox like this:

    Code:
    Dim SR As System.IO.StreamReader = myprocess.StandardOutput
    Results = SR.ReadToEnd
    SR.Close()
    txtResults.AppendText(Results)
    How could I intercept this text, check for the presence of [Warning], then color the entire line yellow?

    TIA

    Rock
    Last edited by Rock_Vacirca; Jan 25th, 2009 at 12:38 PM. Reason: resolved

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] Adding color to text in a RichTextBox

    you'd have to add the text to the rtb line by line. to change the color of the line, select it then set the selectioncolor.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2009
    Posts
    31

    Re: [2005] Adding color to text in a RichTextBox

    I was hoping for something along the lines of:

    If Results contains "[Warning]" then from "[Warning]" to end-of-line Color Yellow

    If Results contains "[Error]" then from "[Error]" to end-of-line Color Red

    etc

    It is these conditional statements that have always defeated me (it is not laziness, I have a specific condition that makes it particularly difficult for me to think in terms of 'Ifs'.

    Any help would be greatly appreciated.

    Rock

  4. #4
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Adding color to text in a RichTextBox

    Hi,

    You can try something like this:

    vb Code:
    1. Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
    2.         Dim cl As Long
    3.         cl = RichTextBox1.SelectionStart
    4.         Dim StartPos As Long
    5.         Dim EndPos As Long
    6.         StartPos = 1 : EndPos = 1
    7.         RichTextBox1.SelectionStart = 1
    8.         RichTextBox1.SelectionLength = Len(RichTextBox1.Text)
    9.         RichTextBox1.SelectionColor = Color.Black
    10.         Me.Text = RichTextBox1.TextLength
    11.         'RichTextBox1.Text
    12.  
    13.  
    14.         Do
    15.  
    16.             StartPos = InStr(EndPos, RichTextBox1.Text, "[Warning", CompareMethod.Binary)
    17.             If StartPos = 0 Then Exit Do
    18.             EndPos = InStr(StartPos, RichTextBox1.Text, ".", CompareMethod.Binary)
    19.             If EndPos = 0 Then Exit Do
    20.             EndPos = EndPos + 1
    21.             RichTextBox1.SelectionStart = StartPos - 1
    22.             RichTextBox1.SelectionLength = EndPos - StartPos
    23.             RichTextBox1.SelectionColor = Color.Yellow
    24.  
    25.         Loop
    26.  
    27.         StartPos = 1 : EndPos = 1
    28.         Do
    29.  
    30.             StartPos = InStr(EndPos, RichTextBox1.Text, Chr(34), CompareMethod.Binary)
    31.             If StartPos = 0 Then Exit Do
    32.             If StartPos = Len(RichTextBox1.Text) Then Exit Do
    33.             EndPos = InStr(StartPos + 1, RichTextBox1.Text, Chr(34), CompareMethod.Binary)
    34.             If EndPos = 0 Then Exit Do
    35.             EndPos = EndPos + 1
    36.             RichTextBox1.SelectionStart = StartPos - 1
    37.             RichTextBox1.SelectionLength = EndPos - StartPos
    38.             RichTextBox1.SelectionColor = Color.Yellow
    39.  
    40.         Loop
    41.  
    42.         Do
    43.  
    44.             StartPos = InStr(EndPos, RichTextBox1.Text, "[Error", CompareMethod.Binary)
    45.             If StartPos = 0 Then Exit Do
    46.             EndPos = InStr(StartPos, RichTextBox1.Text, ".", CompareMethod.Binary)
    47.             If EndPos = 0 Then Exit Do
    48.             EndPos = EndPos + 1
    49.             RichTextBox1.SelectionStart = StartPos - 1
    50.             RichTextBox1.SelectionLength = EndPos - StartPos
    51.             RichTextBox1.SelectionColor = Color.Red
    52.  
    53.         Loop
    54.  
    55.         StartPos = 1 : EndPos = 1
    56.         Do
    57.  
    58.             StartPos = InStr(EndPos, RichTextBox1.Text, Chr(34), CompareMethod.Binary)
    59.             If StartPos = 0 Then Exit Do
    60.             If StartPos = Len(RichTextBox1.Text) Then Exit Do
    61.             EndPos = InStr(StartPos + 1, RichTextBox1.Text, Chr(34), CompareMethod.Binary)
    62.             If EndPos = 0 Then Exit Do
    63.             EndPos = EndPos + 1
    64.             RichTextBox1.SelectionStart = StartPos - 1
    65.             RichTextBox1.SelectionLength = EndPos - StartPos
    66.             RichTextBox1.SelectionColor = Color.Red
    67.  
    68.         Loop
    69.  
    70.         RichTextBox1.SelectionLength = 0
    71.         RichTextBox1.SelectionStart = cl
    72.         RichTextBox1.SelectionColor = Color.Black
    73.  
    74.     End Sub

    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 obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2005] Adding color to text in a RichTextBox

    Mate that's VB6 code, use the Length property instead of Len and Substring instead of InStr. And I think you're missing brackets in lines 16 and 44.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] Adding color to text in a RichTextBox

    @sparrow1. thats a lot of code to change the selectioncolor.

    try this:

    vb Code:
    1. Dim SR As System.IO.StreamReader = myprocess.StandardOutput
    2. Results = SR.ReadToEnd
    3. SR.Close()
    4. txtResults.Text = Results
    5. Dim x As Integer = 0
    6. For Each line As String In txtResults.lines
    7.     If line.StartsWith("[Warning]") Then
    8.         txtResults.SelectionStart = txtResults.GetFirstCharIndexFromLine(x)
    9.         txtResults.SelectionLength = line.Length
    10.         txtResults.SelectionColor = Color.Yellow
    11.     End If
    12.     x += 1
    13. Next

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2009
    Posts
    31

    [Resolved] [2005] Adding color to text in a RichTextBox

    Many thanks, that worked great.

    Rock
    PS You don't fancy having a go at my question in the CodeBank do you, concerning an emulater for a Command Prompt window?

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [Resolved] [2005] Adding color to text in a RichTextBox

    why are you asking questions in the codebank?
    put it in the forum. you'll have more chance of getting it answered...

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