Results 1 to 5 of 5

Thread: Ignoring the CAsE in RichTextBoxFinds

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    3

    Ignoring the CAsE in RichTextBoxFinds

    Hello Devs,

    I am new to VB.net, and to this forum, so please be patient if I make any errors.

    I have a problem with my code below. The RichTextBoxFinds.None doesn't seem to work.

    I have a richtextbox (RichTxt_CV) and a textbox (txtFind_CV). I want the richtext box, to highlight any word in the txtfind_CV textbox, in Red.
    This is almost working, but no matter what I try, I just can't get it to igore the CaSingG of the txtfind_Cv textbox. I think I have tried everything. Any suggestions?


    Code:
        Private Sub HighlightCVText() 'This highlights the searched for word in the CV box
            If txtFind_CV.Text = "" Then GoTo endme
            Dim textEnd As Integer = RichTxt_CV.TextLength
            Dim Textindex As Integer = 0
            Dim lstIndex As Integer = RichTxt_CV.Text.LastIndexOf(txtFind_CV.Text)
    
            While Textindex < lstIndex
                RichTxt_CV.Find(txtFind_CV.Text, Textindex, textEnd, RichTextBoxFinds.None)
                RichTxt_CV.SelectionColor = Color.Red
                Dim bfont As New Font(RichTxt_CV.Font, FontStyle.Bold)
                RichTxt_CV.SelectionFont = bfont
                Textindex = RichTxt_CV.Text.IndexOf(txtFind_CV.Text, Textindex) + 1
            End While
    endme:
        End Sub
    Thanks so much in advance for your help.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Ignoring the CAsE in RichTextBoxFinds

    Code:
            Dim x, y As Integer
            y = 0
            Do
                y = RichTextBox2.Text.IndexOf(TextBox1.Text, y, StringComparison.CurrentCultureIgnoreCase)
                If y <> -1 Then
                    RichTextBox2.Select(y, TextBox1.Text.Length)
                    RichTextBox2.SelectionColor = Color.Red
                    y = y + TextBox1.Text.Length
                    If y + TextBox1.Text.Length >= RichTextBox2.TextLength Then Exit Do
                Else
                    Exit Do
                End If
            Loop
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    3

    Re: Ignoring the CAsE in RichTextBoxFinds

    Thanks for your reply dbasnett

    As I mentioned, I am a newbie to .Net, so I am probably not seeing something obvious, but I tried your code and it just loops continually.

    I notice you don't use the x integer ... could it be something to do with this?

    Thanks again for your help though.

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Ignoring the CAsE in RichTextBoxFinds

    This solution uses regex. Surrounding the word you're search for with \b, tells the parser to match whole words only.

    Code:
        'Imports System.Text.RegularExpressions
        Private Sub HighlightCVText(ByVal word As String)
            Dim wordLength As Integer = word.Length
            Dim excapedWord As String = Regex.Escape(word)
            Dim rx As New Regex("\b" & excapedWord & "\b", RegexOptions.IgnoreCase)
            Dim bFont As New Font(RichTxt_CV.Font, FontStyle.Bold)
            For Each m As Match In rx.Matches(RichTxt_CV.Text)
                Dim textIndex As Integer = m.Index
                RichTxt_CV.Select(textIndex, wordLength)
                RichTxt_CV.SelectionColor = Color.Red
                RichTxt_CV.SelectionFont = bFont
            Next
            RichTxt_CV.SelectionLength = 0
        End Sub
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    3

    Re: Ignoring the CAsE in RichTextBoxFinds

    Thanks so much Wild Bill - you are a genius.

    So sorry that I am only replying now. Was busy with other parts of the project and didn't get a chance to check if anyone had replied.
    This works perfectly.

    For others using this code, don't forget to

    dim word as string = txtFind_CV.Text

    Again, Wild Bill, thanks so much.

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