200 likes | 318 Vues
This lecture focuses on the use of dialog boxes in file management, highlighting the OpenFileDialog, SaveFileDialog, and FolderBrowserDialog controls for user-friendly file and folder selection. Learn how to utilize filters, handle hierarchical relationships between dialog classes, and understand key properties such as CheckFileExists, FileName, Filter, and OverwritePrompt. Gain insight into displaying and managing dialog boxes, including examples of implementing these controls in applications. This content is essential for developers looking to enhance their applications' file manipulation features.
E N D
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 • Using directories to locate and save files
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
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
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
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
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
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;
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
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
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;
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
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
Reading a Special Folder (Example) • Get the special folder corresponding to the Desktop stringdirectoryString; directoryString= Environment.GetFolderPath( Environment.SpecialFolder.Desktop);
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