Download presentation
Presentation is loading. Please wait.
1
Part 3 Saving (without the Save button)
Dictionary Builder Part 3 Saving (without the Save button)
2
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
3
Step 6 – good2go() Private Function good2go(ByVal sender As Object) As Boolean ' Local declarations ' Initialise the flag ' If the data is not saved, ' Issue message box ' Choose action & how to set the flag ' User Cancelled the operation ' Save the text to a file ' Go ahead with Load Return flag End Function
4
Step 6.1 – good2go() ' Local declarations Dim btnPressed As DialogResult Dim proceed As Boolean Const message = "The text has not been saved." _ & vbNewLine & "Save text?" Const caption = "Save text?" Building the message and caption will simplify the call to MessageBox.Show
5
Step 6.2 – good2go() ' Initialise the flag proceed = True
6
Step 6.3 – good2go() ' If the data is not saved, ' Issue message box ' Choose action & how to set the flag ' User Cancelled the operation ' Save the text to a file ' Go ahead with Load
7
Step 6.3 – good2go() ' If the data is not saved, If Not isSaved Then ' Issue message box ' Choose action & how to set the flag ' User Cancelled the operation ' Save the text to a file ' Go ahead with Load End If
8
Step 6.3 – good2go() ' If the data is not saved, If Not isSaved Then ' Issue message box btnPressed = MessageBox.Show(message, caption,_ MessageBoxButtons.YesNoCancel) ' Choose action & how to set the flag ' User Cancelled the operation ' Save the text to a file ' Go ahead with Load End If
9
Step – good2go() ' Choose action & how to set the flag If btnPressed=Windows.Forms.DialogResult.Cancel Then ' User Cancelled the operation ' Save the text to a file ' Go ahead with Load End If
10
Step – good2go() ' Choose action & how to set the flag If btnPressed=Windows.Forms.DialogResult.Cancel Then ' User Cancelled the operation proceed = False ' Save the text to a file ' Don’t Save and go ahead End If
11
Step – good2go() ' Choose action & how to set the flag If btnPressed=Windows.Forms.DialogResult.Cancel Then ' User Cancelled the operation proceed = False Else ' Save the text to a file ' Don’t Save and go ahead End If
12
Step – good2go() ' Choose action & how to set the flag If btnPressed=Windows.Forms.DialogResult.Cancel Then ' User Cancelled the operation proceed = False Else If btnPressed=Windows.Forms.DialogResult.Yes Then ' Save the text to a file ' Don’t Save and go ahead End If
13
Step – good2go() ' Choose action & how to set the flag If btnPressed=Windows.Forms.DialogResult.Cancel Then ' User Cancelled the operation proceed = False Else If btnPressed=Windows.Forms.DialogResult.Yes Then ' Save the text to a file. saveDictionary() ' Don’t Save and go ahead End If
14
Step – good2go() ' Choose action & how to set the flag If btnPressed=Windows.Forms.DialogResult.Cancel Then ' User Cancelled the operation proceed = False Else If btnPressed=Windows.Forms.DialogResult.Yes Then ' Save the text to a file. saveDictionary() ' assign true to the flag isSaved = True ' Don’t Save and go ahead End If
15
Step – good2go() ' Choose action & how to set the flag If btnPressed=Windows.Forms.DialogResult.Cancel Then ' User Cancelled the operation proceed = False Else If btnPressed=Windows.Forms.DialogResult.Yes Then ' Save the text to a file. saveDictionary() ' assign true to the flag isSaved = True End If ' Go ahead with Load
16
Step 6 – good2go() Private Function good2go(sender) As Boolean Dim btnPressed As DialogResult, proceed As Boolean Const message = "The text has not been saved." & vbNewLine & "Save text?" Const caption = "Save text?" ' Initialise the flag variable telling us whether to proceed proceed = True ' If the data is not saved, If Not isSaved Then ' Issue message box prompting whether to do so btnPressed = MessageBox.Show(message, caption, MessageBoxButtons.YesNoCancel) If btnPressed = Windows.Forms.DialogResult.Cancel Then ' User Cancelled the operation proceed = False Else If btnPressed = Windows.Forms.DialogResult.Yes Then ' Save the text to a file. saveDictionary() ' Text has been saved - so assign true to the flag variable. isSaved = True End If Return proceed End Function
17
Step 7 – Saved, but now what?
We’ve saved the words that are in the listBox, but they are still there. Should new words replace them or be added to the list? Who should decide?
18
Step 7 – Saved, but now what?
Clearly, the User should decide what they want done with the words from the file. We can offer them the choice with a different test and a new MessageBox.
19
Step 7 – Saved, but now what?
There are 2 cases in which the choice to Append new words should be offered: When the words were Saved. Windows.Forms.DialogResult.Yes When they weren’t. Windows.Forms.DialogResult.No
20
Step 7 – Saved, but now what?
The third case: when the User cancelled the operation Windows.Forms.DialogResult.Cancel … requires no further action. This accounts for the (perhaps odd) nested If structure in good2go().
21
Step 7 – Saved, but now what?
If btnPressed=Windows.Forms.DialogResult.Cancel Then ' User Cancelled the operation proceed = False Else If btnPressed=Windows.Forms.DialogResult.Yes Then ' Save the text to a file. saveDictionary() ' assign true to the flag isSaved = True End If ' Should the words be appended to the list? **
22
Step 7 – Saved, but now what?
' Should the words be appended to the list? If sender Is btnExit Then ' Not if the Exit button was clicked proceed = False 'so good2go() will fail Else ' Append or Clear? append(proceed) End If
23
Step 7 – Saved, but now what?
We need a sub-program to ask the User whether to append the new words, clear the list first, or maybe Cancel the operation. A job for MessageBox !
24
Step 7 – Saved, but now what?
Since Cancel is a choice, the sub-program may need to modify the proceed flag, in addition to issuing the MessageBox and perhaps clearing the lstWords. proceed is local to good2go()so it must be passed to the sub-program, and since it may be changed it must be passed ByRef.
25
Step 7 – Append or Clear? Private Sub append(ByRef go As Boolean) ' Local declarations ' Initialise flag ' Issue message box prompting to Append or Clear ' Cancel sets the flag to false ' No clears lstWords ' Neither No or Yes changed the flag End Sub
26
Step 7 – Append or Clear? ' Local declarations Dim btnPressed As DialogResult Const message2 = "To add New words to the list - Click Yes“_ & vbNewLine & "To clear the list first - Click No" Const caption2 = "Append/Clear?“ ' Initialise flag ' Issue message box prompting to Append or Clear ' Cancel sets the flag to false ' No clears lstWords ' Neither No or Yes changed the flag
27
Step 7 – Append or Clear? ' Local declarations Dim btnPressed As DialogResult Const message2 = "To add New words to the list - Click Yes“_ & vbNewLine & "To clear the list first - Click No" Const caption2 = "Append/Clear?“ ' Initialise flag go = True ' Issue message box prompting to Append or Clear ' Cancel sets the flag to false ' No clears lstWords ' Neither No or Yes changed the flag
28
Step 7 – Append or Clear? ' Local declarations Dim btnPressed As DialogResult Const message2 = "To add New words to the list - Click Yes“_ & vbNewLine & "To clear the list first - Click No" Const caption2 = "Append/Clear?“ ' Initialise flag go = True ' Issue message box prompting to Append or Clear btnPressed = MessageBox.Show(message2, caption2,_ MessageBoxButtons.YesNoCancel) ' Cancel sets the flag to false ' No clears lstWords ' Neither No or Yes changed the flag
29
Step 7 – Append or Clear? … ' Issue message box prompting to Append or Clear btnPressed = MessageBox.Show(message2, caption2,_ MessageBoxButtons.YesNoCancel) If btnPressed = Windows.Forms.DialogResult.Cancel Then ' Cancel sets the flag to false go = False ' No clears lstWords ' Neither No or Yes changed the flag End If
30
Step 7 – Append or Clear? … ' Issue message box prompting to Append or Clear btnPressed = MessageBox.Show(message2, caption2,_ MessageBoxButtons.YesNoCancel) If btnPressed = Windows.Forms.DialogResult.Cancel Then ' Cancel sets the flag to false go = False ElseIf btnPressed = Windows.Forms.DialogResult.No Then ' No clears lstWords lstWords.Items.Clear() ' Neither No or Yes changed the flag End If
31
Step 7 – Append or Clear? … ' Issue message box prompting to Append or Clear btnPressed = MessageBox.Show(message2, caption2,_ MessageBoxButtons.YesNoCancel) If btnPressed = Windows.Forms.DialogResult.Cancel Then ' Cancel sets the flag to false go = False ElseIf btnPressed = Windows.Forms.DialogResult.No Then ' No clears lstWords lstWords.Items.Clear() ' Neither No or Yes changed the flag End If
32
Step 7 – Append or Clear? Private Sub append(ByRef go As Boolean) Dim btnPressed As DialogResult Const message2 = "To add New words to the list - Click Yes“_ & vbNewLine & "To clear the list first - Click No" Const caption2 = "Append/Clear?" go = True btnPressed = MessageBox.Show(message2, caption2,_ MessageBoxButtons.YesNoCancel) If btnPressed = Windows.Forms.DialogResult.Cancel Then go = False ElseIf btnPressed = Windows.Forms.DialogResult.No Then lstWords.Items.Clear() End If End Sub
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.