The WinForms Notepad Project: Fixing Zoom and Edit Commands (Premium)

In this 11th installment of our start programming project, let's fix zoom and implement several of the Edit menu commands.
Fixing zoom
The way that we originally implemented Zoom In and Zoom Out was somewhat arbitrary and doesn’t resemble the way the new Notepad works: Instead of zooming the view, these menu items increase/decrease the size of the font displayed by TextBox1 by one point (and they only work within a somewhat arbitrary range of 1 to 72 points.) As bad, changing the zoom value (really the font size) doesn’t change the Zoom value in the status bar. And whatever the “zoom” value was set to persist with the app.

As noted earlier, this would have been an easy fix with a RichTextBox, which supports a Zoom property that changes the zoom level of the view, and not the underlying font size. But we’re currently using a TextBox, so we’ll need to work around this. And the workaround I came up with is to maintain the font that persists across application runs in a second property and to update this “master font” property only when required: When the application starts, when the user changes the font (using Format > Font), and when the application closes. That way, we can change the value of TextBox1.Font.Size on the fly using the Zoom In and Zoom Out menu items and not worry about saving the wrong font size when the application closes.

This will require a bit of extra code, of course. And we’re going to need two more Form 1 properties, one to store this “master font” and one to store the current zoom value.  So open Form1.vb, scroll all the way to the top if needed, and add the following two property declarations under the two (for TextHasChanged and DocumentName) that are already there:
Public Shared Property MasterFont As Font
Public Shared Property ZoomValue As Integer = 100
(This proliferation of form properties, which I think of as global variables, may or may not be the most efficient way to handle this. Let me know if you have an opinion about that.)

Next, locate the Form1_Load event handler. And then find the line:
TextBox1.Font = My.Settings.MyFont
Add the following two lines of code below that.
MasterFont = TextBox1.Font
This will duplicate the font used by our text box into the MasterFont property so that it can persist if the user “zooms” the text box display. We have our original font, with its size, stored separately.

Next, find Form1_Closing. We need to ensure that the MasterFont, and not TextBox1.Font, is what’s written to the appropriate app setting. Locate the following line:
My.Settings.MyFont = TextBox1.Font
Now, change this to read:
My.Settings.MyFont = MasterFont
Going forward, we’re going to need to change the display of the zoom level in the status bar as the user changes the “zoom” level and chooses a new font. And right now, that control has a non-memorable name like ToolStripStatusLabel3 or similar. So open Form1.vb [Design], select that field in the st...

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