1 / 20

Lecture Set 12

Lecture Set 12. Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories. Objectives. Use the OpenFileDialog , SaveFileDialog , and FolderBrowserDialog controls, which allow the end user to select disk files and folders Introduction to filters

leigh
Télécharger la présentation

Lecture Set 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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

  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

  4. Hierarchical Relationships Among Dialog Classes

  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

  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

  7. Visual Elements of the Open Dialog Box

  8. The FilterProperty (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

  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

  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;

  11. Filters(A second example) public void EncryptFile() { OpenFileDialogdialog = newOpenFileDialog(); 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

  12. The SaveFileDialogControl • 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

  13. Overwrite Prompt Message

  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;

  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

  16. The FolderBroswerDialog Control

  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

  18. Special Folders

  19. Reading a Special Folder (Example) • Get the special folder corresponding to the Desktop stringdirectoryString; directoryString= Environment.GetFolderPath( Environment.SpecialFolder.Desktop);

  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

More Related