Title | Implement standard File and Help menu commands |
Description | This example shows how to implement standard File and Help menu commands in Visual Basic 6. It implements simple New, Open, Save, Save As, Exit, Help Contents, Help Index, and Help About menu commands. |
Keywords | menus, standard menus, editor |
Categories | Controls |
|
|
This example implements simple New, Open, Save, Save As, Exit, Help Contents, Help Index, and Help About menu commands.
This example is somewhat limited. Search for "@@@" to find places you may want to customize the program.
The program stores the name and title (file name without path) for the loaded data file in the variables FileName and FileTitle. It tracks whether the data is modified with the variable DataModified.
When the user wants to replace the current dadta (start a new file, open an existing file, or close the program), it uses teh DataSafe function to see if the current data is safe to discard. If DataModified is False, then DataSafe simply returns True because there are no changes to save.
If the data has been modified, the program asks the user whether it should save the changes. If the user clicks Yes, the program tries to save the data. It then sets DataSafe to True if the save succeeded.
If the user clicks No, then DataSafe returns True to indicate that the user doesn't want to save the data so it's okay to discard. If the user clicks Cancel, then DataSafe returns False to indicate that it is not safe to discard the changes.
|
|
Private FileName As String ' The full file name.
Private FileTitle As String ' The file name without path.
Private DataModified As Boolean
' Return True if the data is safe.
Private Function DataSafe() As Boolean
' No problem if the data is unmodified.
If Not DataModified Then
DataSafe = True
Exit Function
End If
' See if the user wants to save changes.
Select Case MsgBox("The data has been modified. Do you " & _
"want to save the changes?", vbYesNoCancel)
Case vbYes
' Save the data. Procedure SaveData
' will reset DataModified.
mnuFileSave_Click
DataSafe = Not DataModified
Case vbNo
' Discard the changes to the data.
DataSafe = True
Case vbNo
' Cancel.
DataSafe = False
End Select
End Function
|
|
Subroutine LoadData loads data from a file. If it succeeds, it calls SetDataModified to flag the data as not modified.
Subroutine SaveData saves the data and calls SetDataModified to flag the data as not modified.
|
|
' Load data from the file.
'
' @@@ Modify to load data of the correct format.
Private Sub LoadData(ftitle As String, fname As String)
Dim fnum As Integer
' Open the file.
fnum = FreeFile
Open fname For Input As fnum
' Read all the bytes in the file into the
' TextBox.
EditorText.Text = Input(LOF(fnum), fnum)
' Close the file.
Close fnum
' Save the file name and title.
FileTitle = ftitle
FileName = fname
' Make sure the caption gets updated.
DataModified = True
SetDataChanged False
End Sub
' Save data into the file.
'
' @@@ Modify to save data in the correct format.
Private Sub SaveData(ftitle As String, fname As String)
Dim fnum As Integer
' Open the file.
fnum = FreeFile
Open fname For Output As fnum
' Write text from the TextBox into the file.
Print #fnum, EditorText.Text
' Close the file.
Close fnum
' Save the file name and title.
FileTitle = ftitle
FileName = fname
' Make sure the caption gets updated.
DataModified = True
SetDataChanged False
End Sub
|
|
Subroutine SetDataChanged sets DataModified to True or False and updates the form's caption to show an asterisk if the data has been modified.
|
|
' Set DataModified. Display an asterisk in the
' form's Caption next to the file name if
' appropriate.
Private Sub SetDataChanged(changed As Boolean)
' Don't bother if it's already been done.
If DataModified = changed Then Exit Sub
DataModified = changed
If changed Then
Caption = "Editor*[" & FileTitle & "]"
Else
Caption = "Editor [" & FileTitle & "]"
End If
End Sub
|
|
When the user changes the form's TextBox, its Change event handler calls SetDataChanged to mark the data as changed.
|
|
' Mark the data as modified.
'
' @@@ Call DataChanged whenever the user
' @@@ changes the data.
Private Sub EditorText_Change()
SetDataChanged True
End Sub
|
|
When the user tries to unload the form, the QueryUnload event handler calls the DataSafe function and cancels the unload if the data is not safe.
|
|
' Make sure the data is safe to unload.
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode _
As Integer)
Cancel = Not DataSafe
End Sub
|
|
If the user selects the Help menu's Contents command, the program sets a Common Dialog control's HelpCommand property to cdlHelpContents. It sets the dialog's HelpFile property to the path to the program's help file and calls the dialog's ShowHelp method.
If the user selects the Help menu's Search command, the program sets a Common Dialog control's HelpCommand property to cdlHelpIndex. It sets the dialog's HelpFile property to the path to the program's help file and calls the dialog's ShowHelp method.
See the code for details about the program's other menus work.
|
|
' Show the help contents.
'
' @@@ Create the help file SimpEdit.hlp.
Private Sub mnuHelpContents_Click()
FileDialog.HelpCommand = cdlHelpContents
FileDialog.HelpFile = App.Path & "\SimpEdit.hlp"
FileDialog.ShowHelp
End Sub
' Display the help index so the user can search.
'
' @@@ Create the help file SimpEdit.hlp.
Private Sub mnuHelpSearch_Click()
FileDialog.HelpCommand = cdlHelpIndex
FileDialog.HelpFile = App.Path & "\SimpEdit.hlp"
FileDialog.ShowHelp
End Sub
|
|
|
|