The WinForms Notepad Project: Application Location and Size (Premium)

I was expecting to take a bit of time off from this project because of my Ignite trip, but this was too easy to pass up. Let’s configure NotePadWF to retain the location and size of its application window. Later, we’ll add the ability to save and retain the font that the user chose as well.

Fortunately, this won’t require much code as much of this behavior is built-in to Visual Studio and its application properties interface. To display this interface, right-click the project name in Solution Explorer and choose Properties. (Or, select Project > ProjectName Properties with the Visual Studio menu system.) When the Properties interface appears, select the Settings tab.

We need to add two application settings here, for the application location (position) and size, respectively. So select the field under Name and add a new setting called MyLocation. Then, select the field under Type and select “System.Drawing.Point” from the menu that appears. (You can leave the Scope set to its default, User.)

Now, create a second application setting named MySize and set its Type to “System.Drawing.Size.” Then, save the project and close the Properties interface.

Next, display Form1.vb [Design] and select the form window in the designer so that you can see Form1’s properties in the Properties pane. Locate and then expand “(Application Settings”) at the top of the Properties pane. As you can see, there are two application settings available by default, Location and Text.

Select the carat in the field next to Location and then select MyLocation in the pop-up that appears.

Now, run the application. Move the application window to an obviously different location, and then close it. Then, run the application again. It should appear in the same spot as before.

We’ll probably want to add some error checking when the application starts just in case the location is off-screen. But for now, let’s press forward with the size (MySize) setting. Unfortunately, this time we have to write some code. But it’s simple enough: We’ll need to save the application size when it closes (in the Form1_Closing event hander). And then we’ll need to read the application size and apply it when the application starts up (in the Form1_Load event handler).

We’ve already got some code in each of those event handlers, so we’ll need to add the new code accordingly.

Let’s start with saving the application size in Form1_Closing. Display Form1.vb in Visual Studio and locate Form1_Closing. It has a large If Then Else block that starts with If TextHasChanged Then. So make some room inside the event handler before that line.

Now, add the following two lines of code.
My.Settings.MySize = Me.Size
My.Settings.Save()
Next, locate the Form1_Load event handler. This event handler currently has two lines of code related to fixing the Zoom In and Zoom Out keyboard shortcut menu displays. So make some space after those lines...

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