Analyze the Dialog control in C #

Source: Internet
Author: User
Tags vector font

The dialog box is a special window used in the Widows system to interact with users. In Windows, Common Dialog Boxes are generally divided into the following types based on usage and nature: file opening dialog box, file storage dialog box, font selection dialog box, color selection dialog box, printer Setting dialog box, and file print preview dialog box. As for the presentation and specific functions of these six dialogs in Windows, I think you will be very clear about them. I will not describe them here.
The first thing to note is that the C # language itself does not provide the function to process the dialog box, so the C # Processing dialog box must use "External Force", and this "External Force" is.. Net Framework is provided to all developers. A public class library for. Net platform applications --.. Net FrameWork SDK. The namespace System. Windows. Forms in the. Net FrameWork SDK defines six classes: OpenFileDialog class, SaveFileDialog class, FontDialog class, ColorDialog class, PrintPreviewDialog class, And PrintDialog class. C # uses the preceding six classes to process dialog box-related operations. C # Use the OpenFileDialog class to process operations related to the file selection dialog box; Use the SaveFileDialog class to process and save files; Use the FontDialog class to process and select fonts for the dialog box; use the ColorDialog class processing and color selection dialog box related operations; Use the PrintPreviewDialog class processing and print preview dialog box related operations and use the PrintDialog class processing and printer Setting Dialog Box related operations.

C # The main steps for C # to process the six common Windows dialogs are as follows:

1. instantiate the above class, that is, create an instance of the above class.

2. initialize and display the created instance.

3. process the data returned in the dialog box.

The following describes the composition of these six classes. These six classes have similarities in structure, as some classes have many names and members with the same role. In addition, the method structure of these six classes is relatively simple. The most important method is "ShowDialog", which is to display the dialog box.
1. file selection dialog box:

The file selection dialog box created in C # is actually an instance of the OpenFileDialog class. By configuring
OpenFileDialog class attributes to customize the style and functions of the file selection dialog box. The main attributes of the OpenFileDialog class are as follows:
Attribute
Description
 
AddExtension
This value indicates whether to automatically add an extension to the file name if the user omitting the extension dialog box.
 
CheckFileExists
This value indicates whether a warning is displayed in the dialog box if the user specifies a nonexistent file name.
 
CheckPathExists
This value indicates whether to display a warning if the user specifies a path that does not exist.
 
DefaultExt
Gets or sets the default file extension.
 
DereferenceLinks
This value indicates whether the dialog box returns the location of the file referenced by the shortcut or whether the shortcut is returned.
 
FileName
String to obtain or set a string containing the selected file name in the file dialog box.
 
FileNames
String Array to obtain the names of all selected files in the dialog box.
 
Filter
String to obtain or set the current file name filter string. This string determines the selection content in the "Save As file type" or "file type" dialog box.
 
FilterIndex
Integer. The index of the currently selected filter in the get or set file dialog box.
 
InitialDirectory
String to obtain or set the initial directory displayed in the file dialog box.
 
Multiselect
Boolean indicates whether multiple files can be selected in the dialog box.
 
ReadOnlyChecked
Boolean indicates whether to select the read-only check box.
 
RestoreDirectory
Boolean indicates whether to restore the current directory before the dialog box is closed.
 
ShowHelp
Boolean indicates whether the "help" button is displayed in the file dialog box.
 
ShowReadOnly
Boolean indicates whether the dialog contains the read-only check box.
 
Title
String to obtain or set the title of the file dialog box.
 

 

C # The created instance is displayed by calling the ShowDialog method of the OpenFileDialog class instance, so that a file selection dialog box is displayed.

The following code is used to create an OpenFileDialog instance in C # And set the attribute values of the instance to create a file selection dialog box that can select multiple files, the names of multiple files selected in the dialog box are displayed in the prompt box. Please pay attention to the specific implementation methods:
DialogResult d = openFileDialog1.ShowDialog ();

 

If (d = DialogResult. OK)

{

String [] fileNames = openFileDialog1.FileNames;

String fileNameString = string. Empty;

Foreach (string s in fileNames)

{

FileNameString = fileNameString + s + "";

}

MessageBox. Show (fileNameString );

}
 


Is the format of the file opening dialog box customized by the above Code:

 


Is the data selected in the file selection dialog box shown in the prompt box in the above program, that is, the selected multiple file names:

 

 

2. File Storage dialog box:

C # The Create File Save dialog box uses the SaveFileDialog class. The structure of the SaveFileDialog class is very similar to that of the OpenFileDialog class. The following table lists the main attributes of the SaveFileDialog class and their descriptions:
Attribute
Description
 
AddExtension
This value indicates whether the dialog box automatically adds an extension to the file name if the extension is omitted.
 
CheckFileExists
This value indicates whether the dialog box displays a warning if the user specifies a file name that does not exist.
 
CheckPathExists
This value indicates whether the dialog box displays a warning if the user specifies a path that does not exist.
 
CreatePrompt
This value indicates whether the dialog box prompts the user to allow the creation of a file if the user specifies that the file does not exist.
 
DefaultExt
Gets or sets the default file extension.
 
DereferenceLinks
This value indicates whether the dialog box returns the location of the file referenced by the shortcut or whether the shortcut is returned.
 
FileName
Gets or sets a string containing the selected file name in the file dialog box.
 
FileNames
Obtain the names of all selected files in the dialog box.
 
Filter
Gets or sets the filter string for the current file name. This string determines the selection content in the "Save As file type" or "file type" dialog box.
 
FilterIndex
Obtains or sets the index of the selected filter in the file dialog box.
 
InitialDirectory
Obtain or set the initial directory displayed in the file dialog box.
 
OverwritePrompt
This value indicates whether the "Save as" dialog box displays a warning if you specify an existing file name.
 
RestoreDirectory
This value indicates whether to restore the current directory before closing the dialog box.
 
ShowHelp
This value indicates whether the "help" button is displayed in the file dialog box.
 
Title
Obtain or set the file dialog box title.
 

 

3. font selection dialog box:

C # The FontDialog class is used in the create font selection dialog box. The FontDialog class structure is not complex, and there are many similarities with the two classes described above, as shown in the following table:

 

Attribute
Description
 
AllowScriptChange
Indicates whether the user can change
 
AllowSimulations
Indicates whether the graphic device interface is allowed in the dialog box.
 
AllowVectorFonts
Specifies whether to select a vector font in the dialog box.
 
AllowVerticalFonts
Indicates whether the dialog box displays both vertical and horizontal fonts, or only horizontal fonts.
 
Color
Gets or sets the color of the selected font.
 
FixedPitchOnly
Indicates whether to select only fixed-spacing fonts in the dialog box.
 
Font
Obtains or sets the selected font.
 
FontMustExist
Indicates whether the dialog box specifies the error conditions when the user tries to select a nonexistent font or style.
 
MaxSize
Obtains or sets the maximum pounds that a user can select.
 
MinS

Related Article

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.