Search code examples
vb.netvisual-studio-2010application-settings

How to store & load user preferences to file ? VB 2010


I am writing an application that allows the user to build a report which displays the meta data about a database. It is expected that the user builds the report via the application interface and at any time can save their preferences to file for use in another application,

Now, I have searched around and I have seen articles about application settings and the like, but my question ( I believe ) is some what different:

Say the user has a selection of items they wish to build the report from, how do I save those selections (made by use of radio buttons) to a settings file so that when the program is run again, the user can open up the settings file and allow the previous preferences to be used for another report. I also wish to allow the user to configure the page layout of the report at run time and have that included in the settings file for other uses in the future.

I hope my explanation is adequate, I am having a slight difficulty understanding the task myself because of the over worded nature of the person who set it, but alas we work to what we have!


Solution

  • You can store any serialiable content in your user settings. So just define your user selection class, and your Page layout class, and you can make it persistent like any string or integer.
    (you can see how here : Custom enum as application setting type in C#? )

    Edit : In this post, you can see that you might use any object in your preferences, including one that you defined yourself in your project. To do that, you have to provide the full class name, including the NameSpace of your project (MyNameSpace.MyPreferenceStorageClass) .

    Edit 2 : So i will describe more with little example.
    1) you have to define a class that will store your settings. I choose arbitrary names for my example :

    Public Class UserChoices
        Public Property DisplayInColor As Boolean = False
        Public Property UseKeyboard As Boolean = True
    End Class
    

    Be sure to put a default value. You need to do a successfull compile after writing your class for the class to be inside current namespace.

    2) then go into the Settings Window of your project. Add a settings variable with the Name you want.
    I called it AppUserChoices. Then choose the type, go to 'Browse', then type MyNameSpace.UserChoices as a type.
    (Obviously, replace 'MyNameSpace' by your namespace... ;-) )

    3) you're done. I've written a little code to play with the settings (i put it in the Startup event handler of my Application ):

    Launch it several times. The first time, it should report that user choices are nothing. Then they will be ok, and the color setting will switch each time beetween color and B&W.

       Private Sub Application_Startup
                       (ByVal sender As System.Object, ByVal e As System.Windows.StartupEventArgs)
            If My.Settings.AppUserChoices Is Nothing Then
                MessageBox.Show("AppUserChoices is nothing")
                My.Settings.AppUserChoices = New UserChoices
                My.Settings.Save()
            Else
                MessageBox.Show("AppUserChoices is  **not** nothing")
                My.Settings.AppUserChoices.DisplayInColor = Not My.Settings.AppUserChoices.DisplayInColor
                My.Settings.Save()
                If My.Settings.AppUserChoices.DisplayInColor Then _
                          MessageBox.Show("show colors") _
                    Else _
                          MessageBox.Show("show in B&W")
            End If
        End Sub
    

    4) Note that you might want to have the UserChoices class to implement INotifyPropertyChange, in case you modify them in the code. ( Example : if user changes 'DisplayInColor', you might want to set 'PrintInColor' to the same value. )

    5) for the preference the user has for page layout, create also a class to store the Layout preferences and a new item in the Settings in the same way.

    6) for binding to a settings value, well, let another StackOverflow post do the job : Bind to a value defined in the Settings