You are on page 1of 16

Lecture 4: Dialog Boxes (2)

Introduction
 Last time, we discussed our first two standard Dialog Boxes:
 The MessageBox…
 Including choosing buttons, the default button and an icon.
 The FontDialog Box
 Which supports UI-based changing of Fonts and text properties.

 Now, we continue by discussing two more Dialog Boxes:


 The SaveFileDialog Box
 That allows users to conveniently open files.
 The OpenFileDialog Box
 Which supports easy, UI-based file saving.
Dialog Boxes for File Processing
 In VB I, we discussed various simple Methods for File Processing…
 For File Opening and Closing:
 FileOpen() and FileClose()
 FreeFile()
 For Sequential Output to a file ( OpenMode.Output )
 Print(), PrintLine(), Write(), WriteLine()
 For Sequential Input from a file ( OpenMode.Input )
 Input(), LineInput()
 For Sequentially Appending Output to a file (OpenMode.Append)

 VB .NET also supports UI-based File I/O via a pair of Dialog Boxes:
 The SaveFileDialog Control
 Which uses the StreamWriter Class to do the actual work of writing…
 The OpenFileDialog Control
 Which uses the StreamReader Class to do the actual work of reading…
The SaveFileDialog
 The SaveFileDialog supports UI-based saving to a file…
 Using the standard APIs which are provided by Windows for developers.
 Here, APU = Application Programming Interface
 As a result, we see identical Dialogs from other Applications (e.g., MS Word)
 However, .NET provides it in a convenient VB form…
 So that we don’t have to worry about the specific details of the API.
 The SaveFileDialog can be used in your application as a:
1. VB .NET Class, by…
 Declaring an Instance
in code;
 Setting its properties
in code.
2. VB .NET Control, by…
 Dragging the Control
from the ToolBox.
 Setting its properties in code,
or in the Properties Window.
 Either way, it will have the same methods, properties, and events…
 And will be displayed, as shown above…
 By invoking the ShowDialog() Method.
SaveFileDialog Properties
 Various SaveFileDialog Properties indicate its appearance / behavior:
 Name: name of the Control instance ( as usual ).
 CheckFileExists or CheckPathExists:
 Warn the user if the specified file or path does not exist?
 CreatePrompt
 Ask the user for permission to create a file, if it does not exist?
 FileName:
 Indicates the name of the file selected in the Dialog Box (Read Only).
 Filter:
 Indicates the current filename filter string (e.g., *.txt),
 This determines the file choices that appear in the ‘Files of Type’ ComboBox.
 FilterIndex:
 Indicates the ‘index’ of the filter currently selected in the dialog box.
 Initial Directory:
 The initial directory displayed in the dialog box.
 Overwrite Prompt:
 Warn the user, if the file / path already exists?
 Title: the title displayed in the Dialog Title Bar.
 Etc…
 Each can be set in the Properties Window (for the Control version)…
 Or, directly in code (more flexible).
SaveFileDialog Methods
 Various SaveFileDialog Methods are available for use:
 Dispose():
 releases the current dialog box for trash collection.

 Reset():
 Resets all properties of the instance to their default values
 ShowDialog():
 Shows the DialogBox.
 Example: SaveFileDialog1.ShowDialog()
 The SaveFileDialog returns a result of OK (Save) or Cancel.
 FileName():
 Indicates the name of the file selected in the Dialog Box
 Here, we will be using ShowDialog() and FileName().
The StreamWriter Class
 The SaveFileDialog is only a GUI to help File Processing.
 It does not have the power to write to, and close a file.
 Instead, the actual writing is performed by a StreamWriter
 Which saves data to a file…as directed by the SaveFileDialog.

 Class StreamWriter is implemented by the System.IO namespace.


 Before creating one, you must import System.IO to your project:
 Using the statement: Imports System.IO before your Class Definition.

 A StreamWriter object is created with the following syntax:

Dim myWriter As StreamWriter = New StreamWriter( “myFile.txt”, Boolean a )

 Right side : Creates an Object of type StreamWriter


 Left side : Declares an Object Reference called myWriter to point to a StreamWriter
 Assignment operator : passes a reference/pointer to the Object to myWriter.

 The Constructor has 2 parameters:


 1st Parameter: A String indicating the target file name.
 This will come from your SaveFileDialog.

 2nd Parameter : A Boolean which indicates whether to ‘append’ data.


 True: Append saved data to target file contents.

 False: Replace target file contents with saved data.


SaveFileDialog: Example
 Let’s use the TextEditor from our previous lecture…
SaveFileDialog: Example (cont.)
The OpenFileDialog
 The OpenFileDialog supports UI-based opening of a file…
 Again, using the standard APIs which are provided by Windows …
 However, .NET provides it in a convenient VB form…
 So that we don’t have to worry about the specific details of the API.

 The OpenFileDialog can also be used in your application as a:


1. VB .NET Class, by…
 Declaring an Instance
in code;
 Setting its properties
in code.
2. VB .NET Control, by…
 Dragging the Control
from the ToolBox.
 Setting its properties in code,
or in the Properties Window.
 Either way, it will have the same methods, properties, and events…
 And will be displayed, as shown above…
 By invoking the OpenFileDialog’s ShowDialog() Method.
OpenFileDialog Properties
 OpenFileDialog’s Properties are similar to that of SaveFileDialog:
 Name: name of the Control instance ( as usual ).
 AddExtension / DefaultExt:
 Indicates the default filename extension / if one is added, if omitted by the user.
 CheckFileExists / CheckPathExists:
 Is the user warned if the specified file / path does not exist?
 FileName:
 Indicates the name of the file selected in the Dialog Box.
 Filter:
 Indicates the current filename filter string (e.g., *.txt)…
 this determines the file choices that appear.
 Initial Directory:
 The initial directory displayed in the bialog box.
 MultiSelect:
 May the user select multiple files?
 Note: In this case, the FileNames Property indicates all selected files (Read-
Only).
 Title: the title displayed in the Dialog Title Bar.
 Each can be set in the Properties Window (for the Control version)…
 Or, directly in code (more flexible).
OpenFileDialog Methods
 As a UI, SaveFileDialog supports the same methods as SaveFileDialog:
 Dispose():
 releases the current dialog box for trash collection.
 Reset():
 Resets all properties of the instance to their default values
 ShowDialog():
 Shows the DialogBox.
 Example: OpenFileDialog1.ShowDialog()
 The OpenFileDialog returns a result of OK (Open) or Cancel.
 Here, we will be using ShowDialog().
The StreamReader Class
 Again, the OpenFileDialog is only a GUI to help File Processing.
 It does not have the power to open and read a file.
 Instead, the actual work is performed by a StreamReader
 Which opens a file, and reads data…as directed by the OpenFileDialog.

 Class StreamReader is implemented by the System.IO namespace.


 Before creating one, you must import System.IO to your project:
 Using the statement: Imports System.IO before your Class Definition.

 A StreamReader object is created with the following syntax:

Dim myReader As StreamReader = New StreamReader( “myFile.txt” )

 Right side : creates a new Object of type StreamReader


 Left side : declares a new Object Reference for a StreamReader Object.
 Assignment operator : passes a reference to the Object to myReader.

 The Constructor has only 1 parameter:


 A String, here shown as “myFile.txt”, indicating the target file name.
 This will come from your OpenFileDialog.
OpenFileDialog: Example
 Let’s again use the TextEditor from our previous lecture…
OpenFileDialog: Example (cont.)
Conclusion
 In this lecture set, we have discussed four standard Dialog Boxes:
 The MessageBox…
 Including choosing buttons, the default button and an icon.
 The FontDialog
 Which supports UI-based changing of Fonts and text properties.
 The SaveFileDialog
 That allows users to conveniently open files.
 The OpenFileDialog
 Which supports easy, UI-based file saving.

 Several more Dialogs are available for GUI-based File Processing:


 The PrintDialog Control (and associated Dialog Boxes)
 Supports basic GUI-based printing of text.
 The FolderBrowserDialog Control
 Supports selection of a Folder, rather than a File for processing.

 However, instead we next move on, to discuss Menu capability:


 With the MenuStrip and ContextMenuStrip Controls

You might also like