A while back Welcometomylair1 asked a question about saving project settings so they would persist from one run to the next. In my reply I outlined a way to extend the existing SessToken.txt file to save project settings as well as the session token. G-Factor suggested a simpler way of doing this using VB’s inbuilt application settings facility. In this step we look at this.
1. Saving the value of a control’s property.
For example the Text property of a TextBox.
TextBoxes are handy controls which are commonly used to hold settings for your project, e.g. marketId, sample frequency, etc. For this exercise drag a TextBox from the ToolBox onto the TestForm and rename it to tParam (or anything you like).
With this TextBox selected so that its properties appear in the "Properties" widow, expand the (ApplicationSettings) node. Click on (PropertyBindings) then click (...) to show the "Applications Setting for tParam" form. Select the Text property then click (New...) in the property’s associated drop down list window to show the "New Application Setting" form. Enter these settings:
DefaultValue: 123 Name: Param1 Scope: User
Click OK, OK. With this procedure we have bound the tParam.Text property to an application setting we have created named Param1. Think of an application setting as being a special variable whose value is retained from one run to the next.
Now run the project. Notice that the default value we entered appears in the TextBox. Change this value to something else (e.g. 456), then close the form to stop the project running. Re-run the project and notice that the new value is now in the TextBox. Thus the TextBox always retains the most recently entered value. We have achieved this without writing any code.
2. Saving any value.
The application settings can be used to retain any value you like. You can create a new setting by clicking "BetfairX Properties" on the Project menu to show the Properties form. Select the "Settings" tab. Here you will see all the settings used by the project, including Param1 which we added above. You add a new setting by adding a new entry to the table. For example, add the name Param2, having value "This is Param2". This setting becomes a property of the My.Settings object and can be accessed from anywhere in you code with:
My.Settings.Param2
To demonstrate, put this code in Sub TestForm_Load:
Code:
.....
Print("Last run: " & My.Settings.Param2) 'Previous run time
My.Settings.Param2 = Now 'Re-assign the setting
Print("This run: " & My.Settings.Param2) 'Current run time
.....
and run the project a few times. You will see that Param2 initially holds the previous run time.
Note:
We could have used an application setting to hold the session token rather than storing it in the auxiliary file SessToken.txt.
Two "scopes" are available. With "User" scope the values of the settings apply only to the current (Windows) user. With "Application" scope the settings values apply to all users.