locked
ComboBox - Saving added items at runtime

    Question

  • Here's what I have which adds text typed into the combo box to be added to the combobox as an item at runtime on a button click:

     

    Dim ni As String = cboDocType.Text

    cboDocType.Items.Add(ni)

    cboDocType.Text = ""

     

    But this language doesn't add the items permanently to the combo box as part of the collection. When I stop the program then run it again, the items are gone. How do I make them permanently show as items in the combobox at runtime??

    Tuesday, June 12, 2007 7:55 PM

Answers

  • I wrote an article about how to do this recently: Code Example: Using Binary Serialization

     

    The article describes how to use binary serialization and there's a code example of saving the contents of a Listbox which you could easily modify for a combobox.

    Tuesday, June 12, 2007 8:43 PM
  • Since the objects are being added at runtime to begin with, you'll need to serialize the data to disk before the program exits, and then load it back when the program starts.  You can facilitate this process by using an intermediate container for your values and then binding the combobox to the list.  Here is a basic example that lets the user add to the combobox items by clicking a button and then saves/loads those values when the form opens and closes:

    Code Snippet

    Public Class Form1

    Private mComboBoxValues As New List(Of String)

    Private mBindingSource As New BindingSource

    Private mSaveFile As String = "c:\comboboxvalues.dat"

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    If System.IO.File.Exists(Me.mSaveFile) Then

    Dim fs As System.IO.FileStream = System.IO.File.Open(Me.mSaveFile, IO.FileMode.Open)

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

    Me.mComboBoxValues = CType(bf.Deserialize(fs), List(Of String))

    fs.Close()

    End If

    Me.ComboBox1.DataSource = Me.mBindingSource

    Me.mBindingSource.DataSource = Me.mComboBoxValues

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    Dim fs As System.IO.FileStream = System.IO.File.Open(Me.mSaveFile, IO.FileMode.OpenOrCreate)

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

    bf.Serialize(fs, Me.mComboBoxValues)

    fs.Close()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Me.mComboBoxValues.Add(String.Format("New Item {0}", Me.mComboBoxValues.Count))

    Me.mBindingSource.ResetBindings(False)

    End Sub

    End Class

     

     

    Create a new form, add a combobox and a button and then drop in this code.  Change the directory for mSaveFile as well.

     

    That should get you started.

    Tuesday, June 12, 2007 8:55 PM
    Moderator
  • Just call a Remove method on the BindingSource.  Add a second button to the example form and then add this code:

    Code Snippet

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    If Not Me.mBindingSource.Current Is Nothing Then

    Me.mBindingSource.RemoveCurrent()

    End If

    End Sub

     

    That should get it!

    Wednesday, June 20, 2007 12:11 AM
    Moderator

All replies

  • I wrote an article about how to do this recently: Code Example: Using Binary Serialization

     

    The article describes how to use binary serialization and there's a code example of saving the contents of a Listbox which you could easily modify for a combobox.

    Tuesday, June 12, 2007 8:43 PM
  • Since the objects are being added at runtime to begin with, you'll need to serialize the data to disk before the program exits, and then load it back when the program starts.  You can facilitate this process by using an intermediate container for your values and then binding the combobox to the list.  Here is a basic example that lets the user add to the combobox items by clicking a button and then saves/loads those values when the form opens and closes:

    Code Snippet

    Public Class Form1

    Private mComboBoxValues As New List(Of String)

    Private mBindingSource As New BindingSource

    Private mSaveFile As String = "c:\comboboxvalues.dat"

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    If System.IO.File.Exists(Me.mSaveFile) Then

    Dim fs As System.IO.FileStream = System.IO.File.Open(Me.mSaveFile, IO.FileMode.Open)

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

    Me.mComboBoxValues = CType(bf.Deserialize(fs), List(Of String))

    fs.Close()

    End If

    Me.ComboBox1.DataSource = Me.mBindingSource

    Me.mBindingSource.DataSource = Me.mComboBoxValues

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    Dim fs As System.IO.FileStream = System.IO.File.Open(Me.mSaveFile, IO.FileMode.OpenOrCreate)

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

    bf.Serialize(fs, Me.mComboBoxValues)

    fs.Close()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Me.mComboBoxValues.Add(String.Format("New Item {0}", Me.mComboBoxValues.Count))

    Me.mBindingSource.ResetBindings(False)

    End Sub

    End Class

     

     

    Create a new form, add a combobox and a button and then drop in this code.  Change the directory for mSaveFile as well.

     

    That should get you started.

    Tuesday, June 12, 2007 8:55 PM
    Moderator
  • Works perfectly!

     

    For general interest, and working the other way, if someone were to select (at runtime) a combolist item that is bound to the dat file, how would one remove items from the combo list?

    Tuesday, June 19, 2007 7:43 PM
  • Just call a Remove method on the BindingSource.  Add a second button to the example form and then add this code:

    Code Snippet

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    If Not Me.mBindingSource.Current Is Nothing Then

    Me.mBindingSource.RemoveCurrent()

    End If

    End Sub

     

    That should get it!

    Wednesday, June 20, 2007 12:11 AM
    Moderator
  • Thanks Reed!
    Wednesday, June 20, 2007 2:29 PM