This was from an assignment I had to do a few terms ago. Basically what we had to do was take contents from a list box and save them to a text file. Then later open that same text file and add the contents to the combo box. Also if someone added something to the box and then tried to close it, a message would have to pop up asking them if they wish to save their changes. Anyways this is how it is done.

Since we are using input output we need to use the import statement for it.

Imports System.IO
Declare our module level variables:
 Private isdirty As Boolean
    Dim FileOpen As StreamReader
    Dim Filesave As StreamWriter
Now for adding some text to a combo box:

Dim indexinteger As Integer = 0
        Dim foundBoolean As Boolean = False
        Dim listcomparestring As String
        Dim textcomparestring As String

        If catalogsComboBox.Text = "" Then
            MessageBox.Show("Please enter an item to be added to the list", "Error")


            Do While Not foundBoolean And indexinteger < catalogsComboBox.Items.Count
                listcomparestring = catalogsComboBox.Items(indexinteger).ToString().ToUpper
                textcomparestring = catalogsComboBox.Text.ToUpper
                If listcomparestring.Trim = textcomparestring.Trim Then
                    catalogsComboBox.SelectedIndex = indexinteger
                    foundBoolean = True
                    MessageBox.Show("Already in the list")

                    indexinteger += 1

                End If


            If catalogsComboBox.Items.Count = indexinteger Then
            End If
            isdirty = True
        End If

Let me explain what this is doing.

When you enter text it will scan through the current items in the combobox, if there is a match it will not let you add it in (so this is great for weeding out possible duplicate entries).

This also checks to make sure something is actually in the box before adding it, thus removing the chance of the user inputting blank data.

If there has been a change added to the combobox then
  isdirty = True
Basically meaning that this is telling the program for when you goto close it that since it is true, it will prompt the use to save or continue on with closing without saving. That will come later in this post.

Now for saving the file without closing:
 Dim numberOfItems As Integer
        Filesave = My.Computer.FileSystem.OpenTextFileWriter("./data.txt", False)
        numberOfItems = catalogsComboBox.Items.Count - 1
        For indexinteger As Integer = 0 To numberOfItems
        isdirty = False
This is pretty much a basic clean cut way to save text. isdirty is now equal to false since you saved your changes and if you were to close this it would simply close without prompting the user anything.

Opening the textfile:
        Dim responsedialogueResult As DialogResult
        Dim opendataFile As New OpenFileDialog
        Dim datastring As String

        With opendataFile
            .InitialDirectory = Directory.GetCurrentDirectory
            .FileName = "data.txt"
            .Title = "Select file or directory file"
            responsedialogueResult = .ShowDialog

        End With
        If responsedialogueResult <> Windows.Forms.DialogResult.Cancel Then
            FileOpen = New StreamReader(opendataFile.FileName)


                Do Until FileOpen.Peek = -1
                    dataString = FileOpen.ReadLine
            Catch ex As Exception
            End Try

        End If
This adds each line to the combobox through a loop and reads till it hits the end of file (basically when it doesn't see anymore text or there isn't a line after, blank lines in most cases do count so be careful if you manually edit the file in a text editor.

Now for saving when using the Form_closing event:
    Private Sub Form_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim responsedialogueResult As DialogResult
        Dim messagestring As String = "List has changed, would you like to save?"
        If isdirty Then
            responsedialogueResult = MessageBox.Show(messagestring, "List Changed", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If responsedialogueResult = Windows.Forms.DialogResult.Yes Then
                SaveFileToolStripMenuItem_Click(sender, e)
            End If
        End If

    End Sub
This is seeing if isdirty = true and if it doe a dialogue box will prompt the user if they wish to save. If they choose yes, we then call the save sub but calling the button we originally used to save it in the first place (no sense in rewriting something we already created to save). Sender ,e is basically saying oh hey you clicked on me so im going to execute this area of code.