VB icon

Save Form Size, the right way!

Email
Submitted on: 1/25/2015 12:16:00 PM
By: Elias Barbosa (from psc cd)  
Level: Beginner
User Rating: By 11 Users
Compatibility: VB 6.0
Views: 694
 
     Microsoft provided a pretty lame example of saving the Form Size on Registry. This example is on the MSDN Library. In 20 minutes I was able to write a few lines of code that will save not only the Form Size but also the WindowState! The WindowState is a commonly forgotten property that should always be saved. A very surprising example of this mistake can be found on Adobe Illustrator 10! When I Maximize Illustrator's window and, then, close the application, it doesn't memorize the WindowState. When I reopen Illustrator, its window fills the entire screen but it is on Normal WindowState. Because of this, the window is always a little bit off the right position. I always end up maximizing the window. Every single time!! Don't do the same mistake! Just follow the instructions below and you will be home free! :) ============================================================ Update 05/31/02 - 3:22AM! (My wife hates me!!) :) ============================================================ I heard the crowd and made considerable changes on the code. Now it checks the screen size so, in the rare event that the user reduces the resolution of his monitor, the Form will not end up off screen or too big. I, also, fixed a little bug on the Form Unload event. (What would you expect from a code that took me only 20 minutes to write!!) :) ============================================================ Update 05/31/02 - 11:16AM! (Now, my boss hates me, too!!) :) ============================================================ I just made a few tweaks on the code. I can't help it. I am a perfectionist! :) The code will, now, use the name of the Form to create the name of the keys that are going to be created on Registry.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Save Form Size, the right way!
' Description:Microsoft provided a pretty lame example of saving the Form Size on Registry. This example is on the MSDN Library. 
In 20 minutes I was able to write a few lines of code that will save not only the Form Size but also the WindowState! 
The WindowState is a commonly forgotten property that should always be saved. A very surprising example of this mistake can be found on Adobe Illustrator 10! When I Maximize Illustrator's window and, then, close the application, it doesn't memorize the WindowState. When I reopen Illustrator, its window fills the entire screen but it is on Normal WindowState. Because of this, the window is always a little bit off the right position. I always end up maximizing the window. Every single time!!
Don't do the same mistake! Just follow the instructions below and you will be home free! :)
============================================================
Update 05/31/02 - 3:22AM! (My wife hates me!!) :)
============================================================
I heard the crowd and made considerable changes on the code. Now it checks the screen size so, in the rare event that the user reduces the resolution of his monitor, the Form will not end up off screen or too big.
I, also, fixed a little bug on the Form Unload event. (What would you expect from a code that took me only 20 minutes to write!!) :)
============================================================
Update 05/31/02 - 11:16AM! (Now, my boss hates me, too!!) :)
============================================================
I just made a few tweaks on the code. I can't help it. I am a perfectionist! :) 
The code will, now, use the name of the Form to create the name of the keys that are going to be created on Registry.
' By: Elias Barbosa (from psc cd)
'**************************************

'---------------------------------------
'First, get the size that
'the window is supposed
'to have if it is on
'Normal state and, then,
'set the WindowState.
Private Sub Form_Load()
 Dim intScreenHeight As Integer
 Dim intScreenWidth As Integer
 Dim intMeLeft As Integer
 Dim intMeTop As Integer
 Dim intMeWidth As Integer
 Dim intMeHeight As Integer
 
 'Get the monitor Width and Height.
 intScreenHeight = Screen.Height
 intScreenWidth = Screen.Width
 
 'Get the Form dimensions that were
 'previously saved on Registry.
 intMeLeft = GetSetting(App.Title, "Settings", Me.Name & "Left", 100)
 intMeTop = GetSetting(App.Title, "Settings", Me.Name & "Top", 100)
 intMeWidth = GetSetting(App.Title, "Settings", Me.Name & "Width", intScreenWidth / 2)
 intMeHeight = GetSetting(App.Title, "Settings", Me.Name & "Height", intScreenHeight / 2)
 
 'Check if Form would
 'end up off screen.
 If (intMeTop > intScreenHeight) Then
 Me.Top = 100
 Else
 Me.Top = intMeTop
 End If
 
 'Check if Form would end
 'up taller than the screen.
 If (intMeHeight > intScreenHeight) Then
 Me.Height = intScreenHeight / 2
 Else
 Me.Height = intMeHeight
 End If
 
 'Check if Form would
 'end up off screen.
 If (intMeLeft > intScreenWidth) Or (intMeLeft + intMeWidth < 0) Then
 Me.Left = 100
 Else
 Me.Left = intMeLeft
 End If
 
 'Check if Form would end
 'up wider than the screen.
 If (intMeWidth > intScreenWidth) Then
 Me.Width = intScreenWidth / 2
 Else
 Me.Width = intMeWidth
 End If
 
 'Now, set the WindowState.
 Me.WindowState = GetSetting(App.Title, "Settings", Me.Name & "WindowState", vbNormal)
 
End Sub
'---------------------------------------
Private Sub Form_Resize()
 'Only save the window
 'size if it is on Normal
 'WindowState.
 'Check, also, the Visible
 'Property to avoid saving
 'the window size
 'while the Form is
 'loading. This will
 'prevent conflicts.
 If (Me.WindowState = vbNormal) And (Me.Visible) Then
 SaveSetting App.Title, "Settings", Me.Name & "Left", Me.Left
 SaveSetting App.Title, "Settings", Me.Name & "Top", Me.Top
 SaveSetting App.Title, "Settings", Me.Name & "Width", Me.Width
 SaveSetting App.Title, "Settings", Me.Name & "Height", Me.Height
 
 End If
 
End Sub
'---------------------------------------
Private Sub Form_Unload(Cancel As Integer)
 'Save WindowState
 'only if the window is
 'not on Minimized state.
 If (Me.WindowState <> vbMinimized) Then
 SaveSetting App.Title, "Settings", Me.Name & "WindowState", Me.WindowState
 
 End If
 If (Me.WindowState = vbNormal) Then
 'Imagine that the user didn't resize
 'the Form but just moved it and, then,
 'closed it. The new left and top
 'properties would end up not been
 'saved. That's why the following two
 'lines of code are on the Form Resize
 'event and here too.
 SaveSetting App.Title, "Settings", Me.Name & "Left", Me.Left
 SaveSetting App.Title, "Settings", Me.Name & "Top", Me.Top
 
 End If
 
End Sub


Other 8 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.