Setting application settings

I really dont know if I am about to fraize this right or not, but here it goes.

I have got a series of groupboxes on a form with a few comboboxes on each groupbox.

In each combobox there are selections for different operations

What I am thinking about is

how to remember what the user selected last in each combobox, then the next time the the groupbox is shown - return that user to where they were previously with thier selections reguardless of if the application was closed or not.

I used to have a temp table in access to do this, is there a better way without using a lot of resoucres

Can this be hardcoded in the application or is that a no-no

If this is possible can someone point me in the right direction

Davids Learning



Answer this question

Setting application settings

  • Annihil8

    There are a number of answers here, some depending on the version of VS.

    Here's one that works for either: Recursively loop through all the controls on the form and record the control name and selectedindex value. Serialize this data to and from disk.

    Here's a rough example:

    Public Sub SaveComboBoxes(ByVal path As String)

    Dim Data As New System.Collections.ArrayList

    GetAllComboBoxValues(Me.Controls, Data)

    Dim bs As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

    Dim st As System.IO.FileStream = System.IO.File.Create(path)

    bs.Serialize(st, Data)

    st.Close()

    End Sub

    Private Sub GetAllComboBoxValues(ByVal ctrls As System.Windows.Forms.Control.ControlCollection, ByRef data As System.Collections.ArrayList)

    For Each ctrl As System.Windows.Forms.Control In ctrls

    If ctrl.GetType Is GetType(ComboBox) Then

    Dim cbx As ComboBox = DirectCast(ctrl, ComboBox)

    Dim de As New System.Collections.DictionaryEntry

    de.Key = cbx.Name

    de.Value = cbx.SelectedIndex

    data.Add(de)

    End If

    GetAllComboBoxValues(ctrl.Controls, data)

    Next

    End Sub

    Private Sub GetAllComboBoxes(ByVal ctrls As System.Windows.Forms.Control.ControlCollection, ByRef data As System.Collections.ArrayList)

    For Each ctrl As System.Windows.Forms.Control In ctrls

    If ctrl.GetType Is GetType(ComboBox) Then

    data.Add(ctrl)

    End If

    GetAllComboBoxes(ctrl.Controls, data)

    Next

    End Sub

    Public Sub LoadComboBoxes(ByVal path As String)

    Dim Data As New System.Collections.ArrayList

    Dim bs As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

    Dim st As System.IO.FileStream = System.IO.File.Open(path, IO.FileMode.Open)

    Data = CType(bs.Deserialize(st), System.Collections.ArrayList)

    st.Close()

    Dim combos As New System.Collections.ArrayList

    GetAllComboBoxes(Me.Controls, combos)

    For Each de As System.Collections.DictionaryEntry In Data

    Dim cbx As ComboBox

    For Each c As ComboBox In combos

    If c.Name = de.Key Then

    cbx = c

    Exit For

    End If

    Next

    cbx.SelectedIndex = de.Value

    Next

    End Sub

    That should get you started. This could be prettied up a bit (especially in VS05) but it should give you some insight into a workable solution.

    GL!



  • Setting application settings