Results 1 to 7 of 7

Thread: How to remember form size and position when program shuts down while minimized?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    11

    How to remember form size and position when program shuts down while minimized?

    Hi:

    I have a vb.net 2019 winforms application that is designed to run in the systray. During program shutdown, it saves form size and position to a config file. If I have the window open and shut the program down, there is no problem. But when the program shuts down while minimized. the form size and position are bizarre numbers.

    Any ideas on how to get the "Windows state = Normal" numbers while the form is minimized?

    Thanks!!!
    Last edited by montanasoft.com; Jun 13th, 2021 at 12:59 AM.

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    11

    Re: How to remember form size and position when program shuts down while minimized?

    Ok ... I just found the answer. The "RestoreBounds" property will get the size and location of the normal FormWindowState while the window is minimized :

    Code:
    '
    If frmMyForm.WindowState = FormWindowState.Normal Then
         file.WriteLine("FormSize," & frmMyForm.Size.Width.ToString() & _
              "," & frmMyForm.Size.Height.ToString())
         file.WriteLine("FormLocation," & frmMyForm.Location.X.ToString() & _
              "," & frmMyForm.Location.Y.ToString())
    Else
         file.WriteLine("FormSize," & frmMyForm.RestoreBounds.Size.Width.ToString() & _
              "," & frmMyForm.RestoreBounds.Size.Height.ToString())
         file.WriteLine("FormLocation," & frmMyForm.RestoreBounds.Location.X.ToString() & _
             "," & frmMyForm.RestoreBounds.Location.Y.ToString())
    End If
    NOTE: RestoreBounds *only* works when the form is minimized

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,369

    Re: How to remember form size and position when program shuts down while minimized?

    Why not use an Imports or With statement to avoid typing out the full reference to a property or method? Also use interpolated strings. https://docs.microsoft.com/en-us/dot...olated-strings

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,134

    Re: How to remember form size and position when program shuts down while minimized?

    For the record:

    .Size.Width = .Width
    .Size.Height = .Height
    .Location.X = .Left
    .Location.Y = .Top

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,134

    Re: How to remember form size and position when program shuts down while minimized?

    Quote Originally Posted by montanasoft.com View Post
    Ok ... I just found the answer. The "RestoreBounds" property will get the size and location of the normal FormWindowState while the window is minimized :

    Code:
    '
    If frmMyForm.WindowState = FormWindowState.Normal Then
         file.WriteLine("FormSize," & frmMyForm.Size.Width.ToString() & _
              "," & frmMyForm.Size.Height.ToString())
         file.WriteLine("FormLocation," & frmMyForm.Location.X.ToString() & _
              "," & frmMyForm.Location.Y.ToString())
    Else
         file.WriteLine("FormSize," & frmMyForm.RestoreBounds.Size.Width.ToString() & _
              "," & frmMyForm.RestoreBounds.Size.Height.ToString())
         file.WriteLine("FormLocation," & frmMyForm.RestoreBounds.Location.X.ToString() & _
             "," & frmMyForm.RestoreBounds.Location.Y.ToString())
    End If
    NOTE: RestoreBounds *only* works when the form is minimized
    That code could be simplified a lot.
    vb.net Code:
    1. Dim realBounds = If(frmMyForm.WindowState = FormWindowState.Normal,
    2.                     frmMyForm.Bounds,
    3.                     frmMyForm.RestoreBounds)
    4.  
    5. file.WriteLine($"FormSize,{realBounds.Width},{realBounds.Height}")
    6. file.WriteLine($"FormLocation,{realBounds.Left},{realBounds.Top}")
    I wouldn't be separating the Size and Location either:
    vb.net Code:
    1. file.WriteLine($"FormBounds,{realBounds.Left},{realBounds.Top},{realBounds.Width},{realBounds.Height}")

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    11

    Re: How to remember form size and position when program shuts down while minimized?

    jmcilhinney and jmcilhinney ... thanks very much for your suggestions. It's always good to find ways to make the code more streamlined and elegant. I haven't done much coding for the past 20 years, and I'm pretty rusty ... coming from VB6 and still very new to dot-Net. I don't think VB6 had interpolated strings, but I like them. I also like the compact version of the If Statement. I'm sure I'll be using these in the future.

    I'm happy to be coding again and happy to be using this forum.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,134

    Re: How to remember form size and position when program shuts down while minimized?

    Quote Originally Posted by montanasoft.com View Post
    I don't think VB6 had interpolated strings, but I like them.
    String interpolation was only introduced to VB in 2015 or maybe 2017. Not sure whether they were introduced to C# and VB at the same time or VB a version later.
    Quote Originally Posted by montanasoft.com View Post
    I also like the compact version of the If Statement. I'm sure I'll be using these in the future.
    It's basically a native language version of the IIf function that all VB variants have that behaves like the C ternary operator that C# has. The problem with IIf is that it's a function so the arguments you pass must always be evaluated, where the If operator short circuits. That means that this:
    vb.net Code:
    1. Dim a = IIf(b Is Nothing, c, b.d)
    would throw a NullReferenmceException because b.c will be evaluated even if b is Nothing, while this will work as you'd hope:
    vb.net Code:
    1. Dim a = If(b Is Nothing, c, b.d)
    because the last argument is never evaluated if the first argument is True. Likewise the second argument is never evaluated if the first argument is False. There is also a null-coalescing If operator that is less well-known and less often used. This:
    vb.net Code:
    1. Dim x = If(y, z)
    will assign the value of y to x if it is not Nothing and assign z to x if y is Nothing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width