Академический Документы
Профессиональный Документы
Культура Документы
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.
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.