Library code snippets

Using the Tag property for MDI save and change status

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

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Using the Tag property for MDI save and change status.

Leave a comment

Sign in or Join us (it's free).

Daniel Okely -Daniel Okely Inaugural Developerfusion.com Prize Winner

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!