I’ve spent the better part of the past ten days trying to implement some of the more difficult parts of Notepad and keep coming up short. But let’s move forward while I sort through that mess: This week, we’ll improve the scrollbars, clean up the status bar, and see what it looks like to publish our app to other computers.
Scrollbars
Last time, we added scrollbars to the application’s textbox, but the implementation was a bit basic: We just enabled both scrollbars (vertical and horizontal) and left it at that. Worse, this doesn’t reflect how Notepad really works. Notepad always displays the vertical scrollbar, but it only displays a horizontal scrollbar when word-wrap is disabled.
So let’s fix that.
First up, display Form1.vb [Design] and select TextBox1. Then, locate the ScrollBars property in the Properties pane and change it from Both to Vertical.
Now, we need to change this property when the user selects Format > Word Wrap. So, open Form1.vb and locate the WordWrapToolStripMenuItem_Click event handler. It should look something like this:
Private Sub WordWrapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles WordWrapToolStripMenuItem.Click
  If WordWrapToolStripMenuItem.Checked Then
      TextBox1.WordWrap = False
      WordWrapToolStripMenuItem.Checked = False
  Else
      TextBox1.WordWrap = True
      WordWrapToolStripMenuItem.Checked = True
  End If
End Sub
In the If block, add the line:
TextBox1.ScrollBars = ScrollBars.Both
Then, in the Else block, add the line:
TextBox1.ScrollBars = ScrollBars.Vertical
We also need to set TextBox1’s Scrollbars property when the form first loads, and do so after we read the value of My.Settings.MyWordWrap. If you look in Form1_Load, you will see the two following lines of code conveniently located right at the end of the event handler:
TextBox1.WordWrap = My.Settings.MyWordWrap
WordWrapToolStripMenuItem.Checked = My.Settings.MyWordWrap
So we need to add the following code after this, inside of the event handler:
If TextBox1.WordWrap Then
  TextBox1.ScrollBars = ScrollBars.Vertical
Else
  TextBox1.ScrollBars = ScrollBars.Both
End If
That should do it. If you test the app now, the scrollbars should change along with the Word Wrap setting configured by the user, and they should appear correctly when the app launches each time. (I am actually seeing a bug when I disable Word Wrap, close the app, and rerun it. But if I reenable Word Wrap, close, and re-run the app, it works properly. I don’t believe this is a bug in the app, though I could be wrong. If so, I can’t figure out what’s causing it. Let me know if you encounter this and/or have any ideas.)
Status bar cleanup
After a lot of experimentation with both caret positioning, which is not natively supported by the Textbox control, and text file encoding and formatting, which does not work consistently in .NET, I’ve decide...
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.