The WinForms Notepad Project (2022): Fix the Maximize Bug (Premium)

Here’s a quick fix for one of the bugs in .NETpad: It doesn’t correctly remember the window state if you close it while maximized.

I listed this issue in The WinForms Notepad Project (2022): Fork Me: as originally published, the app will remember the size and position of the application window when you close it and restart it. But if the app is maximized when you close it, it will simply appear normally but take up all of the available space. What it should do, of course, is reappear as maximized.

So let’s fix that. This one is easy.

First, we need to create a new global variable. So right-click on the project name in Solution Explorer, choose Properties, and then navigate to Settings. Add a new setting called “Maximized” of type bool and set its default value to False.

Now, we need to edit the Form1_Load() and Form1_Closing() methods to account for this new setting. Let’s start with the latter: find the Form1_Closing() method in Form1.cs. It currently starts like so:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    timer1.Enabled = false;
   
    Properties.Settings.Default.MyLocation = Location;
    Properties.Settings.Default.MySize = Size;

What we need to do is check the state of the window using the form’s WindowState property. If it’s maximized, we will set the Maximized setting to true. If it’s not, we will set it to false.

So add the following lines of code below the timer1 bit:
if (WindowState.Equals(FormWindowState.Maximized))
    Properties.Settings.Default.Maximized = true;
else
Properties.Settings.Default.Maximized = false;
Next, find Form1_Load(). It currently starts like so:
private void Form1_Load(object sender, EventArgs e)
{       
    Text = Application.ProductName;
    aboutToolStripMenuItem.Text = "About " + Application.ProductName;
   
    Location = Properties.Settings.Default.MyLocation;
    if (!Properties.Settings.Default.MySize.IsEmpty)
    {
        Size = Properties.Settings.Default.MySize;
    }

What we need to do here is check the Maximized setting. If it’s true, we’ll set the form’s WindowState to FormWindowState.Maximized and ignore the Location and Size settings. If it’s false, we will apply Location and Size settings values to the form’s location and size as before. So the beginning of this method will now look like so:
private void Form1_Load(object sender, EventArgs e)
{       
    Text = Application.ProductName;
    aboutToolStripMenuItem.Text = "About " + Application.ProductName;
   
    if (Properties.Settings.Default.Maximized == true)
  {
        WindowState = FormWindowState.Maximized;
    }
    else
    {
        Location = Properties.Settings.Default.MyLocation;
        if (!Properties.Settings.Default.MySize.IsEmpty)
        {
            Size = Pr...

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