Visual Basic Code Source
Free and Unique code for Visual Basic
 Current page : Home      Custom My.Settings Tutorial



 

Create custom 'MY.MySettings' Properties to memorize Application - Size and Position



 



    This small tutorial is to show you how you can create your own properties to display under the 'My.MySettings' Class. This article will focus on adding Two(2x) new properties/settings called: ApplicationSize and ApplicationPosition. I will use them to store the applications position and Size when it exits or closes. Then the next time the application/program loads, it will use these settings and remember the exact Position and Size from the last time it was opened and closed. This is a potentially nice feature that you can add to your applications to give a more user-friendly experience.

    Once you have your project loaded, goto the 'Project' menu and select the 'Properties' option from the list, which is usually the last item listed. Then select the "Settings" Tab item. You will then see the settings grid which is used to configure the application settings.

Name: This is where you put the name of your property/setting. For this tutorial, put in ApplicationPosition as a name, and ApplicationSize as another name. ApplicationSetting and ApplicationSize will actually be listed under: My.MySettings.Default.

Type: Here you can select the type you want your setting/property to be. Example: Point, Size, Date, Integer, and so on. For the article, set the type for ApplicationPosition to be: 'Point'. For the ApplicationSize set to be the: 'Size' type.

Scope: This is where you can specify if the property/setting can only be modified by your Application, or if the person using your application can change the setting. If 'Application' is set, then the user cannot modify the setting at runtime. If 'User' is selected, then the setting Can be modified at runtime by the user. If you want to have certain preferences that the user can modify, then 'User' is the scope you want to use. For the article I went ahead and set it to 'User'.

Value: You can specify a default value for the property/setting that will be used if the property is being used for the first time or no other value have been specified at runtime. This value will also be used if you 'Reset' the properties. Example: My.MySettings.Default.Reset() will reset the settings to your original values.

    If you are following the article as a exercise, then your Project Properties should look similar to the picture below.

 



    Under the: My.Settings.Default interface, you should have: My.MySettings.Default.ApplicationPosition and My.MySettings.Default.ApplicationSize available as properties that can now be used. To get your application to use these properties and enable it to remember the size and position to load, you will want to put a few lines of code in the Form_Load event, and the Form_FormClosing event.

Form_Load Event put:

        Me.Location = My.MySettings.Default.ApplicationPosition

        Me.Size = My.MySettings.Default.ApplicationSize



Form_FormClosing Event put:

        My.MySettings.Default.ApplicationPosition = Me.Location

        My.MySettings.Default.ApplicationSize = Me.Size



    Thats all there is to it. Now your application will have the ability to memorize the position and size when it was previously ran and can load in the same position and at the same size. You do NOT need to specifically use the registry or create a ini file to enable these user-friendly capabilities. 

    There ARE more features and properties available for creating custom properties/settings, but those will have to be covered in a future article. Hope you got something worthwhile from this small turorial :) 

    Click Here for a example application with this article. 

                Jason - Edit: 2007, May 20th
 
Visual Basic Code Source - Copyright 1999-2007