Top Banner

of 16

Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

May 30, 2018

Download

Documents

curlicue
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    1/16

    Lecture 4: Dialog Boxes (2)

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    2/16

    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.

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    3/16

    Dialog Boxes for File Processing

    In VB I, we discussed various simple Methods forFile Processing ForFile Opening and Closing:

    FileOpen() and FileClose() FreeFile()

    ForSequential Output to a file ( OpenMode.Output ) Print(), PrintLine(), Write(), WriteLine()

    ForSequential Input from a file ( OpenMode.Input ) Input(), LineInput()

    ForSequentially 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 StreamWriterClass to do the actual work of writing The OpenFileDialog Control

    Which uses the StreamReaderClass to do the actual work of reading

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    4/16

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    5/16

    SaveFileDialog Properties

    Various SaveFileDialog Properties indicate its appearance / behavior: Name: name of the Control instance ( as usual ). CheckFileExists orCheckPathExists:

    Warn the user if the specified file orpath 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).

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    6/16

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    7/16

    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 fileas directed by the SaveFileDialog.

    Class StreamWriteris implemented by theSystem.IOnamespace. Before creating one, you must import System.IO to your project:

    Using the statement: Imports System.IO before your Class Definition.

    A StreamWriterobject is created with the following syntax:

    Dim myWriterAs StreamWriter = New StreamWriter( myFile.txt, Boolean a )

    Right side : Creates an Object of type StreamWriter

    Left side : Declares an Object Reference called myWriterto point to a StreamWriter

    Assignment operator : passes a reference/pointer to the Object to myWriter.

    The Constructorhas 2 parameters: 1st Parameter: A String indicating the target file name.

    This will come from yourSaveFileDialog.

    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.

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    8/16

    SaveFileDialog: Example

    Lets use the TextEditorfrom our previous lecture

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    9/16

    SaveFileDialog: Example (cont.)

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    10/16

    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 dont have to worry about the specific details of the API.

    The OpenFileDialogcan also be used in your application as a:1. VB.NET Class, by

    Declaring an Instancein code;

    Setting its propertiesin 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 OpenFileDialogs ShowDialog() Method.

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    11/16

    OpenFileDialog Properties

    OpenFileDialogs Properties are similar to that ofSaveFileDialog: 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).

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    12/16

    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().

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    13/16

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    14/16

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    15/16

  • 8/14/2019 Lecture 4 - Displaying Dialog Boxes (2)(VB 2008)

    16/16

    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