1 / 19

Dictionary Builder

Learn how to save a dictionary in a step-by-step manner using FileStream and StreamWriter objects. Follow along with code examples.

Download Presentation

Dictionary Builder

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dictionary Builder Part 2 Saving the Dictionary

  2. Step 3 – Saving the Dictionary At first glance this seems like simply programming btnSave, but a closer look is required…

  3. 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.

  4. 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.

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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.

  17. 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

  18. 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.

  19. 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

More Related