Results 1 to 1 of 1
11-21-2010, 02:06 PM #1
- Join Date
- Nov 2010
[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.
Private isdirty As Boolean Dim FileOpen As StreamReader Dim Filesave As StreamWriter
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
isdirty = True
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 Filesave.WriteLine(catalogsComboBox.Items(indexinteger)) Next Filesave.Close() isdirty = False
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) Try Do Until FileOpen.Peek = -1 dataString = FileOpen.ReadLine catalogsComboBox.Items.Add(dataString) Loop FileOpen.Close() Catch ex As Exception End Try End If
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