The WPF Notepad Project: Find, Find Next/Previous, Replace/Replace All, Auto Save (Premium)

Let’s add Find, Find Next/Previous, Replace/Replace All, and Auto Save and make the WPF version of .NETpad functionally complete.

First up, Find.
You may recall that I struggled with Find, Find Next, Find Previous, Replace, and Replace All when I was working on the original, Windows Forms version of .NETpad. Fortunately, a reader, Michael Lerch, bailed me out, and I was able to complete that project using his code.

Separate from this, I later adapted that VB code to C# for a Windows Forms/C# version of the application that I never documented here. And then I adapted it again, for this WPF version, which of course uses the custom Input Box we created in the last installment.

As before, Find, Find Next, Find Previous, Replace, and Replace All also require two global variables. So open MainWindow.xaml.cs and add the following at the bottom of our list of global variables at the top of the MainWindow definition.
string FindTextString = "";
int FindLastIndexFound = 0;
We also need two additional helper methods, so let’s add those to the bottom of the MainWindow definition:
private void FindTheText()
    if (FindLastIndexFound > -1)
        TextBox1.Select(FindLastIndexFound, FindTextString.Length);
        MessageBox.Show(this, "Cannot find " + (char)34 + FindTextString + (char)34, Application.Current.MainWindow.GetType().Assembly.GetName().Name, MessageBoxButton.OK, MessageBoxImage.Information);

private void FindTextIndex(int FindFromIndex, bool FindPreviousIndex)
    string text = TextBox1.Text;
    if (FindPreviousIndex == false)
        FindLastIndexFound = text.IndexOf(FindTextString, FindFromIndex);
        if (FindLastIndexFound == -1)
            // If text is not found, try searching from the beginning
            FindLastIndexFound = text.IndexOf(FindTextString, 0);
        FindLastIndexFound = text.LastIndexOf(FindTextString, FindFromIndex);
        if (FindLastIndexFound == -1)
            //  If text is not found, try searching from the end
            FindLastIndexFound = text.LastIndexOf(FindTextString, text.Length - 1);
With that out of the way, we can work on Find itself. And yes, Find---like Find Next, Find Previous, Replace, and Replace All---will be implemented using a WPF command. So, we have a bit of work to do.

First, open MainWindow.xaml and locate the FindMenu tag. Then, add a Command property to it so that it looks like so:
<MenuItem Command="Find" Name="FindMenu" Header="_Find" />
Next, locate the Window.CommandBindings section and add the following command binding:
<CommandBinding Command="Find" Executed="FindCommand_Executed" />
Now, open MainWindow.xaml.cs and create an empty event handler for FindCommand_E...

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Please check our Community Guidelines before commenting

Windows Intelligence In Your Inbox

Sign up for our new free newsletter to get three time-saving tips each Friday

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Thurrott © 2024 Thurrott LLC