Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
120
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

858 Chapter 19 THE MULTIPLE DOCUMENT INTERFACE

Finally, the Replace and Replace All commands are straightforward. They call the Replace() function to replace the word in the Search box with the word in the Replace box. Their code is shown in Listing 19.14.

Listing 19.14: The Replace and Replace All Buttons

Private Sub bttnReplace_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles bttnReplace.Click MDIForm.activeChildForm.Editor.SelectedText = replaceWord.Text bttnFindNext_Click(sender, e)

End Sub

Private Sub bttnReplaceAll_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles bttnReplaceAll.Click Dim srchMode As CompareMethod

Dim srchStart As Integer srchMode = SetSearchMode()

Dim pointerLocation As Integer = _ MDIForm.activeChildForm.Editor.SelectionStart

MDIForm.activeChildForm.Editor.Text = _ Replace(MDIForm.activeChildForm.Editor.Text, _

srchWord.Text, replaceWord.Text, , , srchMode) MDIForm.activeChildForm.Editor.SelectionStart = pointerLocation

End Sub

Ending an MDI Application

In most cases, ending an application with the End statement isn’t necessarily the most user-friendly approach. Before you end an application, you must always offer your users a chance to save their work. Ideally, you should maintain a True/False variable whose value is set every time the user edits the open document (the Change event of many controls is a good place to set this variable to True) and reset every time the user saves the document (with the Save or Save As command).

Handling unsaved data in normal applications is fairly simple, as there’s only one document to deal with. But in an MDI application, you have to cope with several possible scenarios:

The user closes a child window by clicking its Close button. You should detect this condition and provide the same code you’d use with an SDI application.

The user closes a single document by selecting the Close command of the File menu. This situation is easy to handle—it’s just like a normal application.

The user closes the MDI form. If the MDI form is closed, all the open documents will close with it! If losing the edits in a single document is bad, imagine losing the edits in multiple documents.

Therefore, terminating an MDI application with the End statement is unacceptable. First, you need a mechanism to detect whether a document needs to be saved or not. In a text-processing application, you can examine the Modified property of the TextBox control. For other types of applications, you

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

MDI APPLICATIONS: THE BASICS 859

may have to maintain a list of True/False variables, one for each document. When the document is modified, set the corresponding variable to True. When the document is saved, reset it to False to indicate that the document can be closed without being saved first. When the user attempts to close the document, you can examine this variable and act accordingly—prompt the user about saving the document or create a backup copy of the document.

Tip The Modified property of the TextBox is not automatically reset to False when the document is saved. To use the Modified property, you must explicitly set it to False when the document is saved.

When the user closes a document through the Close command, it’s easy to handle the document. Insert the proper code in the Close command’s event handler to detect whether the document being closed contains unsaved data and prompt the user accordingly. When the user clicks the child form’s Close button, the child form’s Closing event is fired, this time by the child form. Finally, when the MDI form is closed, each of the child forms receives the Closing event. In addition, the MDI form’s Closing event is also fired. Normally, there’s no reason to program this event. As long as you handle the Closing event of the child form, no data will be lost.

In the Closing event, you can cancel the operation of closing a document, or the MDI form itself, by settings the e.Cancel property to True.

To close the active child form, execute the following statements (they must appear in the Close command’s Click event handler):

Private Sub FileExit_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles FileExit.Click

Me.Close()

End Sub

The Close method invokes the Closing event of the child form. In this event, you can add code to detect whether the document has been saved or not and close the document or prompt the user accordingly. The Closing event handler shown in Listing 19.15 examines the Modified property of the TextBox control on the active child form. If it’s False, it doesn’t do anything (it allows the child form to be closed). If it’s True, it prompts the user with a message box. If the user agrees to discard the changes, the event handler terminates and the child form is closed. If the user clicks the No button, the event handler sets the e.Cancel property to True, which cancel the form’s Closing event.

Listing 19.15: The Child Form’s Closing Event Handler

Private Sub Form1_Closing(ByVal sender As Object, _

ByVal e As System.ComponentModel.CancelEventArgs) _ Handles MyBase.Closing

If Me.Editor.Modified Then Dim reply As MsgBoxResult

reply = MsgBox(“Document “ & Me.Text & “ was modified but not saved. “ & _ “Discard the edits?”, MsgBoxStyle.YesNo)

If reply = MsgBoxResult.No Then e.Cancel = True

End If End If

End Sub

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com