The WinForms Notepad Project: More Fit and Finish, Plus a Context Menu (Premium)

This time, we’re going to clean up some code, fix some bugs, and see whether a context menu can improve the app.

Let’s dive in.
App startup bugs
This one has been bugging (ahem) me for a while. It’s a little hard to explain, but it’s easy to see if you install Notepad WF on another PC and run it for the first time: Some of the defaults I’d like to see---like the font and status bar (which I’d prefer to be on)---are not correct.

But this is easy to fix. In both cases, I forgot to set a default value for the associated application setting. So all we need to do is open Application Settings, find the correct settings, and add default values. To do so, right-click the project name in Solution Explorer and choose Properties.

MyFont and MyWordWrap have no default values. So let’s configure both. You can choose whatever font you want, of course (I like Consolas, 11 points). And then set MyStatusBar to True, which will cause the status bar to display by default.

Bingo. All is well, and I’m not sure why I didn’t fix that earlier, sorry.
Fixing issues with the text caret position display code
In the Text Cursor (Caret) Position post, I added the same code for changing the display of the text carat position in the status bar to three different event handlers (TextBox1_Click, TextBox1_KeyUp, and TextBox1_TextChanged), and Lerch recommended that I not do that. He’s right, so we’ll fix it.

But first, we need to look at an issue Jordan_Meyer raised: He says that I should handle TextBox1_KeyDown instead of TextBox1_KeyUp to handle those instances in which the user just holds down an arrow key; “the keydown event will fire multiple times at the repeat rate while a key is held down but the key up only fires once a key is released.”

He’s right. If we handle TextBox1_KeyUp and the user holds down an arrow key, the text caret position will only update correctly when they let go of the key. If we handle TextBox1_KeyDown, the text caret position will update in real-time while the key is held down. So let’s fix that.

To do so, open Form1.vb [Design] and double-click on the text box. This will open Form1.vb to the text box’s TextChanged event handler. But it also puts TextBox1 in the middle drop-down above the editor. So use the right-most drop-down to locate the KeyDown event handler. Select that to add an empty TextBox1_KeyDown event handler.

Leave that empty for now and create a new private method called ChangePositionToolStripStatusLabel(). It should look like so:
Private Sub ChangePositionToolStripStatusLabel()
PositionToolStripStatusLabel.Text =
"Ln " +
(TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart) + 1).ToString() +
", Col " +
(TextBox1.SelectionStart - TextBox1.GetFirstCharIndexFromLine(TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart)) + 1).ToString()
End Sub
OK, now we need to call this method from the three event handlers that are cur...

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