Save Forms’ Size and Location at Run Time

Save Forms’ Size and Location at Run Time

You’ve noticed how some apps display forms and toolboxes in the same location and size as when you last closed them. Here’s some simple code that gives your VB app the same effect by using the Registry. First, fill in an appropriate Tag property for your form at design time?something like Main Application Form or Color Tool Box. Then keep a global string constant called ApplicationName that holds the title for your application. It’s used here to distinguish the Registry key, but it can also be used for error messages. Place this line in a module:

 Public Const ApplicationName = "My Application Name"

Finally, place this code in a module:

 Public Sub SaveFormDisplaySettings(frm As Form)	If frm.Tag = "" Then Exit Sub	SaveSetting ApplicationName, frm.Tag & _		" Display Settings", "Top", Str(frm.Top)	SaveSetting ApplicationName, frm.Tag & _		" Display Settings", "Left", Str(frm.Left)	SaveSetting ApplicationName, frm.Tag & _		" Display Settings", "Height", Str(frm.Height)	SaveSetting ApplicationName, frm.Tag & _		" Display Settings", "Width", Str(frm.Width)End SubPublic Sub LoadFormDisplaySettings(frm As Form)	Dim FormSettings As Variant	Dim intSettings As Integer	If frm.Tag = "" Then Exit Sub	FormSettings = GetAllSettings(ApplicationName, frm.Tag & _		" Display Settings")	If IsEmpty(FormSettings) Then Exit Sub	For intSettings = LBound(FormSettings, 1) _		To UBound(FormSettings, 1)	Select Case FormSettings(intSettings, 0)		Case "Left"			frm.Left = Val(FormSettings(intSettings, 1))		Case "Top"			frm.Top = Val(FormSettings(intSettings, 1))		Case "Height"			frm.Height = Val(FormSettings(intSettings, 1))		Case "Width"			frm.Width = Val(FormSettings(intSettings, 1))	End Select	Next intSettingsEnd Sub

Add this line to the Form_Load events of the forms you want to save:

 Call LoadFormDisplaySettings(Me)

Add this line to the Form_Unload events:

 Call SaveFormDisplaySettings(Me)

Note one side effect: These Registry settings remain in the Registry even after the application has been uninstalled. They’re stored at or below HKEY_CURRENT_USERSoftwareVB and VBA Program SettingsMy Application Name.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes