Use the open save file dialog box

Source: Internet
Author: User
Tags save file
C # Study Notes (16): Open the save file dialog box
Guidance:
 
Apart from printpreviewdialog, other dialog classes are derived from the abstract base class commondialog. This base class can be used to manage windows General dialogs.
Applicable scenarios of the dialog box:
1. openfiledialog should be used to allow users to select and browse files to be opened. This dialog box allows you to select only one file or multiple files.
2. With savefiledialog, you can specify a file name and browsing path for the file to be saved.
3. Select a printer and set the print option.
4. Configure the page margins. pagesetupdialog is usually used.
5. printviewdialog is a method for printing and previewing on the screen, and there are some options such as scaling.
6. fontdialog lists all installed Windows fonts, styles, font sizes, and preview effects for each font to select a font.
7. colordialog is used to select the color.
File Dialog Box:
L openfiledialog: open the file dialog box
L savefiledialog: save file dialog box
Open File Dialog Box
Openfiledialog ofd = newopenfiledialog ();
Ofd. showdialog ();
 
A dialog box is displayed.
You can change the title of the dialog box by modifying the title attribute of an object.
Open is displayed. Make the following settings:
Ofd. Title = "title text after setting :)"
Run the program again and we will see the corresponding changes
 
We can also set the initialdirectory attribute to enable the file opening dialog box to be opened in a set default path. Its default value is an empty string, indicating the user's "My Documents" directory. When this dialog box is used in an application for the first time, the files under the "My Documents" directory are displayed, when the dialog box is opened for the second time, the displayed directory is the same as the directory where the last opened file is located.
In actual use, do not write a path. If the path does not exist, an error occurs. To obtain a specific system folder, you can use the static method getfolderpath () of the system. Environment class (). This method accepts an environment. specialfolder enumeration, which defines the system directory of the returned path.
Ofd. initialdirectory = environment. getfolderpath (environment. specialfolder. templates );
Set the file filter to display a specific type of files in the open file dialog box.
Ofd. Filter = "Text Document (*. txt) | *. txt | all files | *. * | Type of the file to be displayed (*. EXE) | *. EXE"
 
If the filter value is set incorrectly, a running exception system. argumentexception and error message "The provided filter string is invalid" are generated ". Spaces are not allowed before and after the filter.
The filterindex attribute specifies the default option in the list box.
Set file validity verification validatenames to verify whether the user input is a valid Windows file name.
Checkpathexists: verify the validity of the path
Checkfileexists: Verify the file Validity
Ofd. validatenames = true
Ofd. checkpathexists = true
Ofd. checkfileexists = true
Custom help information:
When you set the showHelp attribute of an object, a help button is displayed to customize the help information.
Add a Handler through the helprequest event.
Ofd. showHelp = true
Ofd. helprequest + = neweventhandler (ofd_helprequest );
Privatevoidofd_helprequest (objectsender, eventargs E)
{
MessageBox. Show ("My own help information :)");
}
 
Click the Help button to display the help information that I have defined.
 
Setting the multiselect attribute enables multiple files to be opened in the open file dialog box.
Determine which button is clicked in the open file dialog box through the following.
If (OFD. showdialog () = dialogresult. OK)
{
}
Save file dialog box:
Use the title attribute to set the title of the dialog box.
File Extension:
Addextension is a Boolean attribute that defines whether the file extension should be automatically added to the file name entered by the user. If you have already entered a file extension, no other extension will be added. .
If you do not enter a file extension, use the file extension set in the defaultext attribute. If this attribute is blank, the file extension defined in the selected filter is used. If filter and defaultext are set, default is used no matter what the filter is.
Like openfiledialog, it has three attributes: validatenames, checkfileexists, and checkpathexists. The difference is that for savefiledialog, the default value of checkfileexists is false, indicating that a new file name can be provided and saved.
If the createprompt attribute is set to true, you are asked if you want to create a new file.
If the overwriteprompt attribute is set to true, you are asked whether to overwrite an existing file.
After defining a save file dialog box as follows, we can use the following functions to implement the Save As function:
Savefiledialog SFD = newsavefiledialog ();
Privatevoidmifilesaveas_click (objectsender, eventargs E)
{
If (SFD. showdialog () = dialogresult. OK)
{
Stringfilename = SFD. filename;
SaveFile (filename );
}
}
Protectedvoidsavefile (stringfilename)
{
Try
{
Stream stream = file. openwrite (filename );
Using (streamwriter writer = newstreamwriter (Stream ))
{
Writer. Write (textbox1.text );
}
}
Catch (ioexception ex)
{
MessageBox. Show (ex. message, "simple Editor", messageboxbuttons. OK, messageboxicon. Exclamation );
}
}
This article is transferred from
Http://www.cnblogs.com/Bear-Study-Hard/archive/2006/03/09/346154.aspx
Label words:
Dialog Box file extension file name save path set true user input custom filter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.