Opening an Existing Document

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); } function Print() { window.focus(); if(window.print) { window.print(); window.setTimeout('window.close();',5000); } }
Team-Fly    

Special Edition Using Microsoft® Visual Basic® .NET
By Brian Siler, Jeff Spotts
Table of Contents
Chapter 15.  Multiple Document Interface (MDI) Applications


In addition to being able to open a new blank child window, the user should have the capability of opening an existing text file and placing the contents of the file into a new child document. We will accomplish this functionality via the OpenFileDialog control, dlgOpen, that we placed on the form earlier.

Invoking the Open File Dialog Box

The user will expect to be able to open an existing text file by selecting Open from the parent form's File menu. You will accomplish this by adding a single line of code to the Click event handler for the mnuFileOpen menu item:

  1. Make sure you are still in the Code window for frmParent.

  2. Select mnuFileOpen from the Class Name drop-down; then select Click from the Method Name drop-down.

  3. Type the line dlgOpen.ShowDialog() so that the Click event handler looks like the following:

     Private Sub mnuFileOpen_Click(ByVal sender As Object, _      ByVal e As System.EventArgs) Handles mnuFileOpen.Click      dlgOpen.ShowDialog()  End Sub 

Opening the File and Creating a New Child Window

As you already know if you have used the OpenFileDialog control before, the control itself simply gathers information from the user about a file to be opened; it does not actually open a file. We will accomplish the opening of the file by adding code to the FileOK event handler for dlgOpen, which is invoked when the user has selected a file and clicked OK. Because this code will need to utilize file system I/O functionality provided via the System.IO namespace, you must add an Imports statement referencing that namespace in the frmParent class. Follow these steps:

  1. Make sure you are still in the Code window for frmParent.

  2. At the very top of the Code window, even above the Public Class frmParent statement, add the code Imports System.IO. This adds I/O functionality to the frmParent class.

  3. Select dlgOpen from the Class Name dropdown, then select FileOK from the Method Name dropdown.

  4. Type the following code in the FileOK event handler:

     Private Sub dlgOpen_FileOk(ByVal sender As Object, _      ByVal e As System.ComponentModel.CancelEventArgs) Handles dlgOpen.FileOk      Dim f As New frmChild()      Dim nTemp As Integer = FreeFile()      Dim MyFile As StreamReader      f.MdiParent = Me      MyFile = File.OpenText(dlgOpen.FileName)      f.txtMain.Text = MyFile.ReadToEnd      f.txtMain.Select(0, 0)      MyFile.Close()      f.FileName = dlgOpen.FileName      f.TextSaved = True      f.Show()  End Sub 

This code begins by creating a new instance of frmChild and setting its MDIParent property to the parent form (represented by Me), much like the NewChild procedure does. In this case, however, it uses the functionality provided by the System.IO namespace to open the text file specified by the user (as determined by dlgOpen's FileName property). The contents of the text file are used to populate the text box txtMain on the new child form, and the text box's Select method is invoked to ensure that the text is not selected when the user first sees it. After closing the text file, the new child form's FileName property is set to the value provided by the OpenFileDialog control's FileName property (which in turn invokes the child form's FileName property procedure). Next, the child form's TextSaved property is set to True because the text is currently unchanged from what is saved on the disk. Finally, the child form's Show method causes it to become visible.

You can learn more about the System.IO namespace in Chapter 24, "Working with Files."

For more information on using the System.IO namespace, p. 681


    Team-Fly    
    Top
     



    Special Edition Using Visual Basic. NET
    Special Edition Using Visual Basic.NET
    ISBN: 078972572X
    EAN: 2147483647
    Year: 2001
    Pages: 198

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net