190 likes | 202 Views
Learn how to save a dictionary in a step-by-step manner using FileStream and StreamWriter objects. Follow along with code examples.
E N D
Dictionary Builder Part 2 Saving the Dictionary
Step 3 – Saving the Dictionary At first glance this seems like simply programming btnSave, but a closer look is required…
Step 3 – Saving the Dictionary At first glance this seems like simply programming btnSave, but a closer look is required… The file might also be saved when the user clicks btnExit, or btnExtractWords, or btnOpenDictionary.
Step 3 – Saving the Dictionary It seems sensible, then, to create a sub-program to perform all the operations in saving the file. Then we can call it when needed.
Step 3 – Saving the Dictionary PrivateSub saveDictionary() ' Declare the FileStream and StreamWriter object variables ' Set any properties of the SaveFileDialog and show the dialog window. ' If the user presses the Save button ' Create the FileStream and StreamWriter objects, ' write the contents ' and close the objects. EndSub
Step 3 – Saving the Dictionary PrivateSub saveDictionary() ' Declare the FileStream and StreamWriter object variables Dim theFile As IO.FileStream, writer As IO.StreamWriter ' Set any properties of the SaveFileDialog and show the dialog window. ' If the user presses the Save button ' Create the FileStream and StreamWriter objects, ' write the contents ' and close the objects. EndSub
Step 3 – Saving the Dictionary PrivateSub saveDictionary() ' Declare the FileStream and StreamWriter object variables Dim theFile As IO.FileStream, writer As IO.StreamWriter ' Set any properties of the SaveFileDialog and show the dialog window. dlgSaveFile.InitialDirectory = "…" Dim btnPressed AsDialogResult btnPressed = dlgSaveFile.ShowDialog() ' If the user presses the Save button ' Create the FileStream and StreamWriter objects, ' write the contents ' and close the objects. EndSub
Step 3 – Saving the Dictionary PrivateSub saveDictionary() ' Declare the FileStream and StreamWriter object variables Dim theFile As IO.FileStream, writer As IO.StreamWriter ' Set any properties of the SaveFileDialog and show the dialog window. dlgSaveFile.InitialDirectory = "…" Dim btnPressed AsDialogResult btnPressed = dlgSaveFile.ShowDialog() ' If the user presses the Save button If btnPressed = Windows.Forms.DialogResult.OK Then ' Create the FileStream and StreamWriter objects, ' write the contents ' and close the objects. EndIf EndSub
Step 3 – Saving the Dictionary PrivateSub saveDictionary() ' Declare the FileStream and StreamWriter object variables Dim theFile As IO.FileStream, writer As IO.StreamWriter ' Set any properties of the SaveFileDialog and show the dialog window. dlgSaveFile.InitialDirectory = "…" Dim btnPressed AsDialogResult btnPressed = dlgSaveFile.ShowDialog() ' If the user presses the Save button If btnPressed = Windows.Forms.DialogResult.OK Then ' Create the FileStream and StreamWriter objects, theFile = New IO.FileStream(dlgSaveFile.FileName, _ IO.FileMode.Create, IO.FileAccess.Write) writer = New IO.StreamWriter(theFile) ' write the contents ' and close the objects. EndIf EndSub
Step 3 – Saving the Dictionary PrivateSub saveDictionary() Dim theFile As IO.FileStream, writer As IO.StreamWriter dlgSaveFile.InitialDirectory = "…" Dim btnPressed AsDialogResult btnPressed = dlgSaveFile.ShowDialog() If btnPressed = Windows.Forms.DialogResult.OK Then ' Create the FileStream and StreamWriter objects, theFile = New IO.FileStream(dlgSaveFile.FileName, _ IO.FileMode.Create, IO.FileAccess.Write) writer = New IO.StreamWriter(theFile) ' write the contents ' and close the objects. writer.Close() theFile.Close() EndIf EndSub
Step 3 – Saving the Dictionary PrivateSub saveDictionary() … If btnPressed = Windows.Forms.DialogResult.OK Then ' Create the FileStream and StreamWriter objects, theFile = New IO.FileStream(dlgSaveFile.FileName, _ IO.FileMode.Create, IO.FileAccess.Write) writer = New IO.StreamWriter(theFile) ' write the contents Dim indx AsShort For indx = 0 To lstWords.Items.Count - 1 writer.WriteLine(lstWords.Items.Item(indx)) Next ' and close the objects. writer.Close() theFile.Close() EndIf EndSub
Step 3 – Saving the Dictionary PrivateSub saveDictionary() … If btnPressed = Windows.Forms.DialogResult.OK Then theFile = New IO.FileStream(dlgSaveFile.FileName, _ IO.FileMode.Create, IO.FileAccess.Write) writer = New IO.StreamWriter(theFile) Dim indx AsShort For indx = 0 To lstWords.Items.Count - 1 writer.WriteLine(lstWords.Items.Item(indx)) Next writer.Close() theFile.Close() ' set the flag for other Subs isSaved = True ** EndIf EndSub
Step 3a - isSaved The identifier isSaved is a flag. It will be set and tested by several different parts of the program. It should be declared Globally. Private isSaved AsBoolean
Step 3a - isSaved It should also be intialised… PublicSubNew() ' This call is required by the designer. InitializeComponent() ' Add any initialization after the ' InitializeComponent() call. isSaved = True AcceptButton = btnAddWord EndSub
Step 4 – btnSave PrivateSub btnSave_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles btnSave.Click IfNot isSaved Then saveDictionary() Else MessageBox.Show("There's nothing to Save!") EndIf EndSub
Step 5 - btnExit When the User clicks Exit, the program should check to see if the contents of lstWords have been saved. If not, it should provide the opportunity.
Step 5 - btnExit Dim btnPressed AsDialogResult If isSaved Then End Else btnPressed = MessageBox.Show("The text has not been saved." & _ vbNewLine & "Save text?", "Save text?", _ MessageBoxButtons.YesNoCancel) If btnPressed = Windows.Forms.DialogResult.Yes Then ' Save the text to a file. saveDictionary() End ElseIf btnPressed = Windows.Forms.DialogResult.No Then End EndIf EndIf
Step 5a – Testing isSaved It is common that the same test could be required in different contexts. For example, the code for both buttons that read files will need to check isSavedand generate the same MessageBox that was used in btnExit.
Step 5 - btnExit It makes sense to handle these in a sub-program that can be called when needed. PrivateSubbtnExit_Click(…) HandlesbtnExit.Click If good2go(sender) Then saveDictionary() EndIf End EndSub