dcsimg
Results 1 to 2 of 2

Thread: Find and Replace text in TextBox

  1. #1

    Thread Starter
    New Member Gooer's Avatar
    Join Date
    Dec 2015
    Location
    Nairobi
    Posts
    4

    Cool Find and Replace text in TextBox

    I have my main windows form which has a txtClientArea textbox where a user will input his data. So i put a Find command in menu strip which calls another form frmFindandReplace. This frmFindandReplace should find user input text in txtClientArea textbox of my main windows form. Here is what I have done so far, I have used alot of hours browsing the internet for answers but nothing! I need to find every result in txtClientArea (which should be highlighted with different backcolor) with find button and replace all with replace button.

    Code:
     Private Sub findButton_Click(sender As Object, e As EventArgs) Handles findButton.Click
            Dim a As String
            Dim b As String
    
            a = TextBoxSearch.Text
            b = InStr(txtClientArea.Text, TextBoxSearch.Text)
    
            If b Then txtClientArea.Focus()
            txtClientArea.SelectionStart = b - 1
            txtClientArea.SelectionLength = Len(a)
            txtClientArea.ScrollToCaret()
    Thank you people. i hope i can count on you.

  2. #2
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    318

    Resolved Re: Find and Replace text in TextBox

    First, if memory serves you will need to use a RichTextboxBox control for your txtClientArea control

    The code below can be used like so:

    To Search for all strings
    Code:
    Call search(TextBoxSearch.Text, txtClientArea.Text, True)
    Code:
        ''' <summary>
        ''' Search's the client area and highlights the searched text
        ''' </summary>
        ''' <param name="searchValue">Value to search for</param>
        ''' <param name="searchControl">Richtext control</param>
        ''' <param name="completeWord">Matches only complete words if true else finds a reference</param>
        ''' <remarks></remarks>
        Private Sub search(searchValue As String, searchControl As RichTextBox, completeWord As Boolean)
            Dim regPattern As String = String.Format("{1}{0}{1}", searchValue, If(completeWord = True, "\b", ""))
    
            Dim regex As New Regex(regPattern)
    
            For Each valueFound As Match In regex.Matches(searchControl.Text)
                searchControl.Select(valueFound.Index, valueFound.Length)
                searchControl.SelectionColor = Color.Red
            Next
    
        End Sub
    To Replace all searched strings:

    Code:
    Call replaceString(TextBoxSearch.Text, txReplace.Text, txtClientArea, True)
    Code:
    ''' <summary>
        ''' Search's the client area and replaces and matches found
        ''' </summary>
        ''' <param name="searchValue">Value to search for</param>
        ''' <param name="replaceValue">Replacement Value</param>
        ''' <param name="searchControl">Richtext Control</param>
        ''' <param name="completeWord">Matches only complete words if true else finds a reference</param>
        ''' <remarks></remarks>
        Private Sub replaceString(searchValue As String, replaceValue As String, searchControl As RichTextBox, completeWord As Boolean)
            Dim regPattern As String = String.Format("{1}{0}{1}", searchValue, If(completeWord = True, "\b", ""))
    
            If Regex.Match(searchControl.Text, regPattern).Success Then
                searchControl.Text = Regex.Replace(searchControl.Text, regPattern, replaceValue)
                MessageBox.Show("Replacement Complete", "Process Finished", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                Throw New Exception("No search value found")
            End If
        End Sub
    You also need to Import System.Text.RegularExpressions at the top of your code

    Code:
    Imports System.Text.RegularExpressions
    Hope this helps

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width