C # winform dialog box usage

Source: Internet
Author: User

In the dialog box, we often use the following types:
1. The file dialog box (FileDialog) is commonly used in two ways:
Open File Dialog Box (OpenFileDialog)
SaveFileDialog)
2. FontDialog)
3. Color dialog box (ColorDialog)
4. Print the Pre-Export Dialog Box (PrintPreviewDialog)
5. Page settings (PrintDialog)
6. Print the dialog box (PrintDialog)
For more information, see MSDN. Next we will introduce them one by one. During the introduction, I used a self-developed class: File, mainly for File operations.

File Dialog Box (FileDialog)

1. open the file dialog box (OpenFileDialog)

1. The OpenFileDialog control has the following basic attributes:

Initial directory in the InitialDirectory dialog box
The file Filter to display in the dialog box. For example, "text file (*. txt) | *. txt | all files (*. *) | *.*"
FilterIndex: The index of the file filter selected in the dialog box. If the first item is selected, it is set to 1.
Whether to restore the current directory before the RestoreDirectory control dialog box is closed
FileName: The first file displayed in the dialog box or the last selected file
The Title will contain characters in the Title bar of the dialog box.
Whether AddExtension automatically adds the default Extension
CheckPathExists check whether the specified path exists before returning in the dialog box
DefaultExt default Extension
DereferenceLinks: whether to cancel referencing shortcuts before returning from the dialog box
ShowHelp enable the "help" button
The ValiDateNames control dialog box checks whether invalid characters or sequences are not included in the file name.

2. The OpenFileDialog control has the following common events:


FileOk: the event to be processed when you click "open" or "save ".
HelpRequest events to be processed when a user clicks the "help" button

3. OpenFileDialog:

 


You can use the following code to implement the preceding dialog box:

Private void openFileDialogBTN_Click (o B j e c t sender, System. EventArgs e ){
OpenFileDialog openFileDialog = new OpenFileDialog ();
OpenFileDialog. InitialDirectory = "c: \"; // note that c: \ instead of c: is used to write the path here:
OpenFileDialog. Filter = "text file | *. * | C # file | *. cs | all files | *.*";
OpenFileDialog. RestoreDirectory = true;
OpenFileDialog. FilterIndex = 1;
If (openFileDialog. ShowDialog () = DialogResult. OK)
{
FName = openFileDialog. FileName;
File fileOpen = new File (fName );
IsFileHaveName = true;
RichTextBox1.Text = fileOpen. ReadFile ();
RichTextBox1.AppendText ("");
}
}
The File () class is used to execute File operations in the program. I wrote it myself and attached the source code of this class. If you are interested, you can analyze it by yourself.

Ii. Save file dialog box (SaveFileDialog)

The control of the Save file dialog box can be saved in two situations: save, save as, and save as easily. When the file is opened, write an article about the file, here we mainly talk about saving as (SaveAs ).

1. properties of the SaveFileDialog Control


Filter: The file Filter to be displayed in the dialog box. For example, "text file (*. txt) | *. txt | all files (*. *) | *.*"
FilterIndex: The index of the file filter selected in the dialog box. If the first item is selected, it is set to 1.
Whether to restore the current directory before the RestoreDirectory control dialog box is closed
Whether AddExtension automatically adds the default Extension
CheckFileExists
CheckPathExists check whether the specified path exists before returning in the dialog box
The Container determines whether to prompt users when creating files. This parameter is applicable only when ValidateNames is a true value.
DefaultExt default Extension
DereferenceLinks: whether to cancel referencing shortcuts before returning from the dialog box
FileName: The first file displayed in the dialog box or the last selected file
Initial directory of the InitialDirector dialog box
OverwritePrompt controls whether to prompt the user when the current file is to be rewritten. This applies only when ValidateNames is true.
ShowHelp enable the "help" button
The Title will contain characters in the Title bar of the dialog box.
The ValidateNames control dialog box checks whether invalid characters or sequences are not included in the file name.

2. The SaveFileDialog event is as follows:

 

FileOk: the event to be processed when you click "open" or "save ".
HelpRequest events to be processed when a user clicks the "help" button


3. The effect of SaveFileDialog is as follows:

 


4. Use the following sample code to implement

Private void saveAsDialogBTN_Click (o B j e c t sender, System. EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog ();
SaveFileDialog. Filter = "text file | *. * | C # file | *. cs | all files | *.*";
SaveFileDialog. FilterIndex = 2;
SaveFileDialog. RestoreDirectory = true;
If (saveFileDialog. ShowDialog () = DialogResult. OK)
{
If (saveFileDialog. ShowDialog () = DialogResult. OK)
{
FName = saveFileDialog. FileName;
File fSaveAs = new File (fName );
IsFileHaveName = true; file: // The saved file has a name.
FSaveAs. WriteFile (richTextBox1.Text );
}
}
}
In fact, this can be done directly in the VS. net ide environment. To illustrate the problem, I still have a column. A File class library is used. The following is the source code:
File. cs

Using System;
Using System. IO;
Using System. Windows. Forms;
Using System. Text;

Namespace dialog
{
/**////
/// Summary description for File.
///
Public class File
{
String fileName;
Public File (string fileName)
{
This. fileName = fileName;
}

Public string ReadFile ()
{
Try
{
StreamReader sr = new StreamReader (fileName, Encoding. Default );
String result = sr. ReadToEnd ();
Sr. Close ();
Return result;
}
Catch (Exception e) {MessageBox. Show (e. Message );}
Return null;
}

Public void WriteFile (string str)
{
Try
{
StreamWriter sw = new StreamWriter (fileName, false, Encoding. Default );
Sw. Write (str );
Sw. Close ();
}
Catch (Exception e) {MessageBox. Show (e. Message, "An error occurred while saving the file! ");}
}
}
}
FontDialog)

In text processing, we often use fonts. Now let's make the most common font dialog box.

I. Common FontDialog attributes

ShowColor controls whether to display color options
Whether AllowScriptChange displays the character set of the font
Font displayed in the dialog box
AllowVerticalFonts vertical font
Color selected in the dialog box
FontMustExist: whether to display an error when the Font does not exist
Maximum font size that can be selected for MaxSize
Min size: Optional min font size
ScriptsOnly display exclude OEM and Symbol Fonts
Whether the "application" button is displayed in ShowApply.
Whether ShowEffects displays underline, strikethrough, and font color options
Whether the "help" button is displayed for ShowHelp

2. FontDialog event

Apply: the event to be processed when you click "Apply ".
HelpRequest events to be processed when you click "help"

3. FontDialog

 

IV. Implementation Code

Private void fontDialogBTN_Click (o B j e c t sender, System. EventArgs e)
{
FontDialog fontDialog = new FontDialog ();
FontDialog. Color = richTextBox1.ForeColor;
FontDialog. AllowScriptChange = true;
FontDialog. ShowColor = true;
If (fontDialog. ShowDialog ()! = DialogResult. Cancel)
{
RichTextBox1.SelectionFont = fontDialog. Font; // change the Font of the selected text.
}
}
The above Code sets the selected text to the font in the current FontDialog dialog box.

Color dialog box (ColorDialog)

The color pick-up box is also one of our common dialogs. Let's take a look at how the color dialog box is operated in C?

I. Common Properties of the color dialog box (ColorDialog)


AllowFullOpen disable and enable the "custom color" button
Whether FullOpen displays the "custom color" part of the dialog box first
Whether the "help" button is displayed for ShowHelp
Color: The Color displayed in the dialog box.
Any color available for AnyColor display
Whether CustomColors displays custom colors
Can I select solid color only for solidronronly?

2. ColorDialog:

 


III. For the implementation code, see:

Private void colorDialogBTN_Click (o B j e c t sender, System. EventArgs e)
{
ColorDialog colorDialog = new ColorDialog ();
ColorDialog. AllowFullOpen = true;
ColorDialog. FullOpen = true;
ColorDialog. ShowHelp = true;
ColorDialog. Color = Color. Black; // initialize the current text

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.