locked
How do I persist/store user settings using My.Settings in VB.NET? RRS feed

  • Question

  • How do I persist/store user settings using My.Settings in VB.NET?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, April 9, 2009 1:48 PM

Answers

  • Document: Using My.Settings in VB.NET:

    http://msdn.microsoft.com/en-us/library/ms379611(VS.80).aspx

     

    1)   For example: Persist Form BackColor property value.

    Create a New Setting:

    Project menu-> Properties -> Settings ->create a setting as below

    Name         Type                           Scope      Value

    myColor   System.Drawing.Color     User       SkyBlue

     

     Read Settings when Form_Load

        Me.BackColor = My.Settings.myColor

    Change and save Settings

        My.Settings.myColor = Color.AliceBlue

    My.Settings.Save()

     

    Related thread:

    http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/5f759564-1b67-40e7-8a3e-828783699b6a/

     

    2)   In addition, if you want to persist/store collection data (e.g. ListBox.Items, ComboBox.Items), you need to choose System.Collections.Specialized.StringCollection data type. Here is the walkthrough:


                         I.        Project -> Properties -> Settings -> Create a setting variable:

    Name   ListBoxCollection

    Type   System.Collections.Specialized.StringCollection (Browser and locate this type)

    Scope    User         

    Value   (Add at least one item to initialize it)


                       II.        Code sample

    Prerequisites: Drag&drop ListBox1, TextBox1 and Button1 onto Form1.


    Public Class Form1
        ' Load Settings (StringCollection type) to ListBox when opening form   
        Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load For Each item As String In My.Settings.ListBoxCollection ListBox1.Items.Add(item) Next End Sub 'Add item from TextBox to ListBox and Settings Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles Button1.Click If TextBox1.Text <> "" And ListBox1.Items.Contains(TextBox1.Text) = False Then ListBox1.Items.Add(TextBox1.Text) My.Settings.ListBoxCollection.Add(TextBox1.Text) End If End Sub 'Save Settings when closing form Private Sub Form1_FormClosed(ByVal sender _
    As System.Object, ByVal e As _
    System.Windows.Forms.FormClosedEventArgs) _
    Handles MyBase.FormClosed My.Settings.Save() End Sub End Class

     

    Related thread:

    http://social.msdn.microsoft.com/Forums/en/vbide/thread/c6ba477b-ae4c-4614-83ae-3fbdd1bab673/

     

    For more FAQ about Visual Basic Express, please see Visual Basic Express FAQ

     



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, April 9, 2009 1:50 PM