One tricky thing when making a MDI (multiple-document interface) program in Visual Basic is working out if what the user has created in your program has been saved or changed. For example, if you made a mini MDI word-processor you would want to know if a document has been saved or not so you know whether to bring up the 'Save As' dialog box or not. You would also want to know whether or not a document has been changed (or is 'Dirty') so you know whether or not to show a message box asking if the user wants to save changes to their document when close it. This example shows you how.
MDIForm1 represents the name of the MDI parent form. In your 'Save' subroutine type the following code after the code that saves the document.
'save code goes here!!!
MDIForm1.ActiveForm.Tag = "Saved"
MDIForm1.ActiveForm.Text1.Tag = ""
From the above code you will be able to tell whether a document has been saved
or not. Before the part of your 'Save' subroutine type the following code to
check whether or not the document has been saved:
If MDIForm1.ActiveForm. Tag <> "Saved" Then
SaveAs 'Call the SaveAs
'subroutine because the
'dcoument has not been
'saved yet
Exit Sub
End If
'save code goes here!!!
In the Text1_Change event, type the following code:
Text1.Tag = "Dirty"
This allows you to work out the document has been changed. Type this code in the Form_Unload subroutine of the MDI child form with the textbox (Text1) on it.
If MDIForm1.ActiveForm.Text1.Tag = "Dirty" Then
msg="Current document has been changed. Do you want
to save changes?"
buttons = vbYesNoCancel + vbQuestion
response = MsgBox msg, buttons
If response = vbYes Then Save 'do save sub
If response = vbNo Then End 'exit
If response = vbCancel Then Cancel = 1
'set Cancel to a value
'other than zero so the
'program doesn't close.
End If
Comments