Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories."— Presentation transcript:

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

2 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

3 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

4 Slide 4 Hierarchical Relationships Among Dialog Classes 8/25/2013 10:06 AM

5 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

6 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

7 Slide 7 Visual Elements of the Open Dialog Box 8/25/2013 10:06 AM

8 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

9 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

10 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

11 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

12 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

13 Slide 13 Overwrite Prompt Message 8/25/2013 10:06 AM

14 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

15 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

16 Slide 16 The FolderBroswerDialog Control 8/25/2013 10:06 AM

17 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

18 Slide 18 Special Folders 8/25/2013 10:06 AM

19 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

20 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


Download ppt "Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories."

Similar presentations


Ads by Google