Presentation is loading. Please wait.

Presentation is loading. Please wait.

Part 2 Saving the Dictionary

Similar presentations


Presentation on theme: "Part 2 Saving the Dictionary"— Presentation transcript:

1 Part 2 Saving the Dictionary
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
Private Sub 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. End Sub

6 Step 3 – Saving the Dictionary
Private Sub 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. End Sub

7 Step 3 – Saving the Dictionary
Private Sub 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 As DialogResult btnPressed = dlgSaveFile.ShowDialog() ' If the user presses the Save button ' Create the FileStream and StreamWriter objects, ' write the contents ' and close the objects. End Sub

8 Step 3 – Saving the Dictionary
Private Sub 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 As DialogResult 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. End If End Sub

9 Step 3 – Saving the Dictionary
Private Sub 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 As DialogResult 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. End If End Sub

10 Step 3 – Saving the Dictionary
Private Sub saveDictionary() Dim theFile As IO.FileStream, writer As IO.StreamWriter dlgSaveFile.InitialDirectory = "…" Dim btnPressed As DialogResult 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() End If End Sub

11 Step 3 – Saving the Dictionary
Private Sub 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 As Short For indx = 0 To lstWords.Items.Count - 1 writer.WriteLine(lstWords.Items.Item(indx)) Next ' and close the objects. writer.Close() theFile.Close() End If End Sub

12 Step 3 – Saving the Dictionary
Private Sub 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 As Short 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 ** End If End Sub

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 As Boolean

14 Step 3a - isSaved It should also be intialised… Public Sub New()
' This call is required by the designer. InitializeComponent() ' Add any initialization after the ' InitializeComponent() call. isSaved = True AcceptButton = btnAddWord End Sub

15 Step 4 – btnSave Private Sub btnSave_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles btnSave.Click If Not isSaved Then saveDictionary() Else MessageBox.Show("There's nothing to Save!") End If End Sub

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 As DialogResult 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() ElseIf btnPressed = Windows.Forms.DialogResult.No Then End If

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 isSaved and 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. Private Sub btnExit_Click(…) Handles btnExit.Click If good2go(sender) Then saveDictionary() End If End End Sub


Download ppt "Part 2 Saving the Dictionary"

Similar presentations


Ads by Google