The WinForms Notepad Project: Replace, Go To, Fit and Finish, New Name (Premium)

Today, we implement the Replace, Replace All, and Go To commands, improve the fit and finish, and change the application name.

First, the fit and finish.
A bit of fit and finish
In The WinForms Notepad Project: Find, Find Next, and Find Previous (Premium), I used an Input Box to implement the Find command, and I’m going to use it again to implement Replace, Replace All, and Go To. But before doing that, I want to build out the existing Find Input Box a bit and then use that as a model for the Input Boxes to come.

If you open Form1.vb and locate the FindToolStripMenuItem_Click event hander, you’ll find the line of code that displays the Find Input Box when the user selects this command (Edit > Find, or by typing CTRL + F). It currently looks like so:
FindTextString = InputBox("Find what:")
That’s pretty basic. It is, in fact, the simplest form of InputBox that we can use, and it’s missing some functionality. First, we’re not customizing the title bar text of the dialog, which we should. And because we’re not providing any positioning information, Visual Basic/Windows Forms is just displaying this dialog right in the middle of the screen.

But if you look at the documentation for InputBox, you’ll see that we can pass it several optional parameters in addition to the prompt string, which is required. You can pass it a title, and default response, an XPos (the horizontal position of the dialog on-screen), and a YPos (the vertical position of the dialog on-screen). Those are all useful for Find, and they will all be useful for the other InputBoxes that we’re about to create.

The title is obvious enough: It should read as “Find” (no quotes), as it does in Notepad. The default response can be set to the selected text in the main form’s text box; if nothing is selected, it will just be blank. And then for the XPos and YPos, we can programmatically ensure that the InboxBox is displayed over the application and not just in the middle of the display. Find an exact position that makes sense every single time is not possible, so I’ll just position it near the upper left of the main application window, and over the textbox.

To see what this looks like, replace the line of code noted above with the following:
FindTextString = InputBox("Find what:", "Find", TextBox1.SelectedText, Location.X + 200, Location.Y + 300)
Here, I had selected the first instance of the word Power before typing CTRL + F:

So that’s better.

OK, let’s move on to the other new commands.
Replace
In Form1.vb [Design], open the Edit menu in the main form and double-click the Replace menu item to open the ReplaceToolStripMenuItem_Click event handler in Form1.vb. Then add the following code:
CheckTimerInterval()

Dim FindWhat As String = InputBox("Find what:", "Replace", TextBox1.SelectedText, Location.X + 200, Location.Y + 300)
Dim ReplaceWith As String = InputBox("Replace with:", "Replace", "", Location.X + 200, Location.Y + ...

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