lecture set 12 sequential files and structures part a – dialog boxes, filters, directories

20
Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Upload: darcy-matthews

Post on 03-Jan-2016

229 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Lecture Set 12

Sequential Files and Structures

Part A – Dialog Boxes, Filters, Directories

Page 2: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 2

Objectives Use the OpenFileDialog, SaveFileDialog,

and FolderBrowserDialog controls, which allow the end user to select disk files and folders

Introduction to filters Using directories to locate and save files

8/25/2013 10:06 AM

Page 3: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 3

The OpenFileDialog and SaveFileDialog Controls All dialog boxes derive from the CommonDialog

class and share similar features The OpenFileDialog and SaveFileDialog

controls are derived from the CommonDialog class These classes allow the end user to select a file

to open or save The FolderBrowserDialog control also derives

from the CommonDialog class This control allows the end user to select a

folder

8/25/2013 10:06 AM

Page 4: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 4

Hierarchical Relationships Among Dialog Classes

8/25/2013 10:06 AM

Page 5: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 5

Members of the OpenFileDialog and SaveFileDialog Classes The CheckFileExists and CheckPathExists

properties control whether the end user can select a file or folder that does not exist

The FileName property contains the filename selected by the end user

The Filter property defines the type of files that will be displayed for selection

The FilterIndex property contains the index of the current filter

The Filter and FilterIndex properties work together

8/25/2013 10:06 AM

Page 6: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 6

Members of the OpenFileDialog and SaveFileDialog Classes (continued)

The InitialDirectory property contains the initial folder where the search for files begins

The OverwritePrompt property applies only to the SaveFileDialog; if True, the end user will be prompted before a file is overwritten

The RestoreDirectory property defines whether the current directory will be restored after the end user selects a file

The ShowDialog method displays the respective dialog box

8/25/2013 10:06 AM

Page 7: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 7

Visual Elements of the Open Dialog Box

8/25/2013 10:06 AM

Page 8: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 8

The Filter Property (Introduction) The Filter property restricts the files

displayed based on a file extension A filter consists of

A description Followed by a vertical bar Followed by the actual filter

Multiple filters can be connected together

Do not put spaces between the vertical bars 8/25/2013 10:06

AM

Page 9: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 9

The Filter Property (Syntax)

Object.Filter = [description1|filter1|description2|filter2 ...] description1 contains the description of

the filter filter1 contains the filter itself

The characters '*.' precede the three-character file extension of the filter

Use '*.*' to select all files Vertical bars separate each description,

filter pair

8/25/2013 10:06 AM

Page 10: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 10

The Filter Property (Example)

Set the Filter to three possible filters (*.txt), (*.rtf), and (*.*)

Set the FilterIndex to select the second filter by default

ofdMain.Filter = "Text files (*.txt)|*.txt|" "Rich text files (*.rtf)|*.rtf|”

"All files (*.*)|*.*“;ofdMain.FilterIndex = 2;

8/25/2013 10:06 AM

Page 11: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 11

Filters (A second example)

public void EncryptFile() { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|

*.*"; dialog.InitialDirectory = @"C:\"; dialog.Title = "Please select an image file to encrypt."; if (dialog.ShowDialog() == DialogResult.OK) { //Encrypt the selected file. I'll do this later. :) } // end if} // end EncryptFile

8/25/2013 10:06 AM

Page 12: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 12

The SaveFileDialog Control

The SaveFileDialog control works the same way as the OpenFileDialog control

Set the OverwritePrompt property to True to prevent the end user from accidentally overwriting files

8/25/2013 10:06 AM

Page 13: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 13

Overwrite Prompt Message

8/25/2013 10:06 AM

Page 14: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 14

Displaying the SaveFileDialog (Example) Display an instance of the SaveFileDialog control named sfdMain

If the user clicks OK, store the filename in the text box named txtFileName

DialogResult result;sfdMain.OverwritePrompt = true;result = sfdMain.ShowDialog();if (result == Windows.Forms.DialogResult.OK) txtFileName.Text = sfdMain.FileName;

8/25/2013 10:06 AM

Page 15: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 15

The FolderBrowserDialog (Introduction) Use the FolderBrowserDialog to browse for

folders instead of files Members

The Description property contains the text appearing in the title bar

The RootFolder property contains the topmost folder appearing in the dialog box

The SelectedPath property contains the folder selected by the end user

The ShowDialog method displays the dialog box

8/25/2013 10:06 AM

Page 16: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 16

The FolderBroswerDialog Control

8/25/2013 10:06 AM

Page 17: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 17

Using Windows Defined Directories Members of the System.Environment

class are used to get system directories The SystemDirectory property gets the

Windows system directory The directory is typically C:\Windows\

System The CurrentDirectory property contains

the directory from which the application was run

The GetFolderPath method gets a system special folder 8/25/2013 10:06

AM

Page 18: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 18

Special Folders

8/25/2013 10:06 AM

Page 19: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 19

Reading a Special Folder (Example)

Get the special folder corresponding to the Desktop

string directoryString;directoryString = Environment.GetFolderPath( Environment.SpecialFolder.Desktop);

8/25/2013 10:06 AM

Page 20: Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories

Slide 20

Using Application-defined Directories The Application class contains

application defined directories The StartupPath property contains the

directory from which the application was started

The ExecutablePath property contains the startup path and executable filename

The UserAppDataPath and LocalUserAppDataPath properties return application data directories

8/25/2013 10:06 AM