Giter Club home page Giter Club logo

wpfsearchandhighlighttext's Introduction

WpfSearchAndHighlightText

Search and highlight text in wpf (Rich Text Box)

This is a simple Wpf app, where it has a text box to enter the text we want to search. The app contain a rich text box, where you can post your text. Enter the text to search and click 'Search' button. This will find all the matches and highlights them in yellow.

Also, in status bar at bottom, it will show how many matched found or no matches found.

Method used to search and highlight: (Upon Search button click)

private void bnt_Search_Click(object sender, RoutedEventArgs e)
{
    TextRange textRange = new TextRange(rich.Document.ContentStart, rich.Document.ContentEnd);

    //clear up highlighted text before starting a new search
    textRange.ClearAllProperties();
    lbl_Status.Content = "";

   //get the richtextbox text
   string textBoxText = textRange.Text;

    //get search text
    string searchText = searchTextBox.Text;

    if (string.IsNullOrWhiteSpace(textBoxText) || string.IsNullOrWhiteSpace(searchText))
    {
        lbl_Status.Content = "Please provide search text or source text to search from";
    }

    else
    {

        //using regex to get the search count
        //this will include search word even it is part of another word
        //say we are searching "hi" in "hi, how are you Mahi?" --> match count will be 2 (hi in 'Mahi' also)

        Regex regex = new Regex(searchText);
        int count_MatchFound = Regex.Matches(textBoxText, regex.ToString()).Count;

        for (TextPointer startPointer = rich.Document.ContentStart;
                    startPointer.CompareTo(rich.Document.ContentEnd) <= 0;
                        startPointer = startPointer.GetNextContextPosition(LogicalDirection.Forward))
        {
            //check if end of text
            if (startPointer.CompareTo(rich.Document.ContentEnd) == 0)
            {
                break;
            }

            //get the adjacent string
            string parsedString = startPointer.GetTextInRun(LogicalDirection.Forward);

            //check if the search string present here
            int indexOfParseString = parsedString.IndexOf(searchText);

            if (indexOfParseString >= 0) //present
            {
                //setting up the pointer here at this matched index
                startPointer = startPointer.GetPositionAtOffset(indexOfParseString);

                if (startPointer != null)
                {
                    //next pointer will be the length of the search string
                    TextPointer nextPointer = startPointer.GetPositionAtOffset(searchText.Length);

                    //create the text range
                    TextRange searchedTextRange = new TextRange(startPointer, nextPointer);

                    //color up 
                    searchedTextRange.ApplyPropertyValue(TextElement.BackgroundProperty, new  SolidColorBrush(Colors.Yellow));

                    //add other setting property

                }
            }
        }

        //update the label text with count
        if (count_MatchFound > 0)
        {
            lbl_Status.Content = "Total Match Found : " + count_MatchFound;
        }
        else
        {
            lbl_Status.Content = "No Match Found !";
        }
    }
}
    

wpfsearchandhighlighttext's People

Contributors

manasmodak avatar

Stargazers

 avatar  avatar  avatar  avatar

wpfsearchandhighlighttext's Issues

How to clear search?

Unfortunately, textRange.ClearAllProperties() removes all properties, not only the highlighting. The only way I have found to leave the other properties alone is to keep a copy of the original text and reload it each time a search is performed. This can take a lot of time for larger files.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.