Hey wonderful guest! Please take a minute and register for an account here at codeleakers.com. It takes less than a minute and you will feel better about yourself and life in general. If you do not wish to register, you are still free to browse the site. Thanks!

[VB.NET] Opening and Saving Files From a Combo Box

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.

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")
catalogsComboBox.Focus()
Else
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")
Else
indexinteger += 1
End If
Loop
If catalogsComboBox.Items.Count = indexinteger Then
catalogsComboBox.Items.Add(catalogsComboBox.Text.Trim)
ClearFocus()
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

Code:

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.

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.

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:

Code:

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.