The WinForms Notepad Project: Find, Find Next, and Find Previous (Premium)

I’ve been working on the Find, Find Next, Find Previous, and Replace/Replace All commands for well over a month now. There are lots of challenges to implementing this functionality, especially for a non-programmer such as myself. But there are also some important challenges related to implementing this functionality in the same manner as the original Notepad. And I’ve found myself butting up against my own limitations again and again as a result.

For an inkling to what I mean, consider an earlier issue I had in trying to duplicate the dialog box that Notepad provides when you go to exit the application with an unsaved document. My original assumption was that this dialog was created specifically for Notepad, and that I would have to manually create a reasonable facsimile of it (a tedious task). But as readers informed me, it’s actually a Task Dialog, a UI that was first implemented for Windows Vista. Task Dialogs are not natively supported by Windows Forms for some reason, though Microsoft had at one time made them available via a separate download that is no longer officially available. So my eventual workaround was to simply use a Message Box; it doesn’t look exactly like the dialog that the real Notepad uses, but it is at least a clean, native UI, and it provides exactly the same functionality (in this case). Problem solved.

Now consider how Microsoft implements Find, Find Next, Find Previous, and Replace in Notepad. The Find and Replace commands trigger the two custom dialog boxes shown below. They appear over the main application window and as you interact with them you can cause text to be highlighted and change in the text box in the main application window, in real time; that is, the dialogs stay floating above the main window as you use them. And there are other complexities, including when to even make these commands available; for example, if there is no text in Notepad, then the commands are all grayed out.

To implement these things correctly, I’d have to manually recreate each dialog in Visual Studio, which, again, is tedious. I’d have to handle all of their various nuances---the “Match case” and “Wrap around” options, for example. And I’d have to figure out how to keep these dialogs visible while highlighting or changing the text in the main application’s textbox.

In preparation for this work, I did create the Find dialog to some reasonable degree. And I did figure out how to find text in the textbox. But keeping that dialog displayed while the user clicked “Find Next” repeatedly to move through each instance of that text was one issue I ran into.

After wasting a lot of time on this part of the problem, I finally created a version with a Find panel built-in to the main application window, the theory being that it would be easier to highlight text as the user clicked Find Next, since it was all in the same window. And I did make some headway with that design.

But then a...

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