Results 1 to 7 of 7

Thread: [RESOLVED] Strange Things Happen when colouring particular string in Line of Multiine Richtextbo

  1. #1

    Thread Starter
    Banned
    Join Date
    May 2021
    Posts
    180

    Resolved [RESOLVED] Strange Things Happen when colouring particular string in Line of Multiine Richtextbo

    Hello

    Strange things happens When I try to color particular String in a line of Multiline RichTextbox

    Frist of all Colour whch i try to implement using color.FromARGB(205, 133, 63) it should show as Peru as defined in
    https://condor.depaul.edu/sjost/it23...colorNames.htm

    1. But it shows some other color as Blue in GroupA
    2. In GroupB for Text "Dark Brown" following Letters "ow" shows Black and other letters in same as above Blue instead of colour Saddle Brown

    Is the above link correct to obtain the correct RGB codes

    Secondly i would completely like to upgrade the function ColorForLine(..........) or Function CheckLineColorsOfRichTextBox(....)

    Where I would like to pass the color reference for Color.stringColorName as well Color.FromARGb(RGBcolor)

    i've gone through https://www.vbforums.com/showthread....g-color-to-RGB

    somehow has gone above my head

    Have managed to adapt but not Happy for re-writing the code
    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim groupA() As String = {"The", "Quick", "Brown", "Fox", "Jumped", "Over", "The", "Lazy", "Dogs"}
            Dim groupB() As String = {"The", "Quick", "Dark Brown", "Fox", "Jumped", "Over", "The", "Lazy", "Dogs"}
    
            RichTextBox1.Text = (String.Join(Environment.NewLine, groupA.ToArray))
            Call CheckLineColorsOfRichTextBox(RichTextBox1, "Brown", RGB(205, 133, 63)) 'Brown  'Peru Color =205, 133, 63
    
            RichTextBox2.Text = (String.Join(Environment.NewLine, groupB.ToArray))
            Call CheckLineColorsOfRichTextBox(RichTextBox2, "Dark Brown", RGB(139, 69, 19)) '(160, 82, 45)) 'Sienna 'Saddle Brown
    
        End Sub
    
        Private Sub CheckLineColorsOfRichTextBox(ByRef currentRichTextbox As RichTextBox, ByRef txtStr As String, ByVal rgbcolor As Long)
            Dim i As Integer = 0
            For Each Line As String In currentRichTextbox.Lines
                currentRichTextbox.Select(RichTextBox1.GetFirstCharIndexFromLine(i), Line.Length)
                currentRichTextbox.SelectionColor = ColorForLine(Line, txtStr, rgbcolor)
                i += 1
            Next
        End Sub
    
    Private Function ColorForLine(Line As String, ByRef txtStr As String, RGBcolor As Long) As Color
    Dim HiliteColor As Color = Nothing
        If Line.Contains(txtStr) Then
                Return Color.FromArgb(RGBcolor)
            Else
                'Return Color.Black
                If HiliteColor = Nothing Then
                    HiliteColor = Color.Black
                End If
            End If    
    End Function
    Thanks
    SamD
    177
    Last edited by SamDsouza; Apr 20th, 2024 at 03:16 AM.

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,066

    Re: Strange Things Happen when colouring particular string in Line of Multiine Richte

    try it this way, it uses Regex so you can also use patterns to search and color

    Code:
    Imports System.Text.RegularExpressions
    
    Public Class Form13
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
            Dim groupA() As String = {"The", "Quick", "Brown", "Fox", "Jumped", "Over", "The", "Lazy", "Dogs"}
            Dim groupB() As String = {"The", "Quick", "Dark Brown", "Fox", "Jumped", "Over", "The", "Lazy", "Dogs"}
    
            RichTextBox1.Text = (String.Join(Environment.NewLine, groupA.ToArray))
            'Call CheckLineColorsOfRichTextBox(RichTextBox1, "Brown", RGB(205, 133, 63)) 'Brown  'Peru Color =205, 133, 63
            Mark(RichTextBox1, "Brown", Color.Brown)
    
    
            RichTextBox2.Text = (String.Join(Environment.NewLine, groupB.ToArray))
            'Call CheckLineColorsOfRichTextBox(RichTextBox2, "Dark Brown", RGB(139, 69, 19)) '(160, 82, 45)) 'Sienna 'Saddle Brown
            Mark(RichTextBox2, "Dark Brown", Color.SaddleBrown)
    
    
        End Sub
    
        Private Sub Mark(ByVal rtb As RichTextBox, ByVal word As String, ByVal color As Color)
            Dim matchc As MatchCollection = Regex.Matches(rtb.Text.ToLower(), word.ToLower())
            For Each m As Match In matchc
                rtb.Select(m.Index, m.Length)
                rtb.SelectionColor = color
                rtb.Select(rtb.Text.Length, 0)
                rtb.SelectionColor = Drawing.Color.Black
            Next
        End Sub
    
    
        'Private Sub CheckLineColorsOfRichTextBox(ByRef currentRichTextbox As RichTextBox, ByRef txtStr As String, ByVal rgbcolor As Long)
        '    Dim i As Integer = 0
        '    For Each Line As String In currentRichTextbox.Lines
        '        currentRichTextbox.Select(RichTextBox1.GetFirstCharIndexFromLine(i), Line.Length)
        '        currentRichTextbox.SelectionColor = ColorForLine(Line, txtStr, rgbcolor)
        '        i += 1
        '    Next
        'End Sub
    
        'Private Function ColorForLine(Line As String, ByRef txtStr As String, RGBcolor As Long) As Color
        '    Dim HiliteColor As Color = Nothing
        '    If Line.Contains(txtStr) Then
        '        Return Color.FromArgb(RGBcolor)
        '    Else
        '        'Return Color.Black
        '        If HiliteColor = Nothing Then
        '            HiliteColor = Color.Black
        '        End If
        '    End If
        'End Function
    End Class
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3

    Thread Starter
    Banned
    Join Date
    May 2021
    Posts
    180

    Re: Strange Things Happen when colouring particular string in Line of Multiine Richte

    ChrisE

    You Rock

    Indeed a step further considering with Regex pattern

    I tried it works perfectly. But when i tried to incorporate the same.with Bold why its not happening. It should work

    Code:
    Private Sub Mark(ByVal rtb As RichTextBox, ByVal word As String, ByVal color As Color)
            Dim matchc As MatchCollection = Regex.Matches(rtb.Text.ToLower(), word.ToLower())
            For Each m As Match In matchc
                rtb.Select(m.Index, m.Length)
                rtb.SelectionColor = color
                rtb.Select(rtb.Text.Length, 0)
                rtb.SelectionColor = Drawing.Color.Black
                rtb.SelectionFont = New Font(rtb.Font, FontStyle.Bold)
            Next
        End Sub
    Thanks
    SamD
    178

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,066

    Re: Strange Things Happen when colouring particular string in Line of Multiine Richte

    move that line to....

    Code:
      Private Sub Mark(ByVal rtb As RichTextBox, ByVal word As String, ByVal color As Color)
            Dim matchc As MatchCollection = Regex.Matches(rtb.Text.ToLower(), word.ToLower())
            For Each m As Match In matchc
                rtb.Select(m.Index, m.Length)
                rtb.SelectionFont = New Font(rtb.Font, FontStyle.Bold)
                rtb.SelectionColor = color
                rtb.Select(rtb.Text.Length, 0)
                rtb.SelectionColor = Drawing.Color.Black
            Next
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5

    Thread Starter
    Banned
    Join Date
    May 2021
    Posts
    180

    Re: Strange Things Happen when colouring particular string in Line of Multiine Richte

    ChrisE

    move that line to....
    Code:
    Private Sub Mark(ByVal rtb As RichTextBox, ByVal word As String, ByVal color As Color)
            Dim matchc As MatchCollection = Regex.Matches(rtb.Text.ToLower(), word.ToLower())
            For Each m As Match In matchc
                rtb.Select(m.Index, m.Length)
                rtb.SelectionFont = New Font(rtb.Font, FontStyle.Bold)
                rtb.SelectionColor = color
                rtb.Select(rtb.Text.Length, 0)
                rtb.SelectionColor = Drawing.Color.Black
            Next
        End Sub
    Any specific reason that we had to move that line in the beginning.

    Resolved

    Thank you very much

    I sincerely request the Moderators to change the Spelling of Multiine to Multiline of this thread topic
    SamD
    179

  6. #6
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,066

    Re: [RESOLVED] Strange Things Happen when colouring particular string in Line of Mult

    Hi sam
    the first 3 Lines take care of the search and format (Color and in this case the Font -> Bold)
    Code:
                rtb.Select(m.Index, m.Length)
                rtb.SelectionFont = New Font(rtb.Font, FontStyle.Bold)
                rtb.SelectionColor = color
    then back to Color Black and goto the next word
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7

    Thread Starter
    Banned
    Join Date
    May 2021
    Posts
    180

    Re: [RESOLVED] Strange Things Happen when colouring particular string in Line of Mult

    ChrisE

    Thanks for your remarks. Indeed Appreciated

    SamD
    180

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