Visual C # dialog box (1)

Source: Internet
Author: User
  ViewArticle  
Visual C # dialog box (1) 2008-04-29
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.
File Dialog Box (filedialog)

1. open the file dialog box (openfiledialog)

1. The openfiledialog control has the following basic attributes:

The file filter to display in the initial directory filter in the initialdirectory dialog box, for example, "text file (*. TXT) | *. TXT | all files (*. *) | *. * "the index of the file filter selected in the filterindex dialog box, if you select the first item, set it to 1 restoredirectory control dialog box, whether to restore the current directory filename, the first file displayed in the dialog box or the last selected file title will be displayed in the title bar of the dialog box whether the character addextension automatically adds the default extension checkpathexists
Check whether the default defaultext extension dereferencelinks exists in the specified path before returning in the dialog box.
Enable the "help" button validatenames control dialog box to check 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: the event to be processed when you click "help ".

 

You can use the followingCodeTo implement the preceding dialog box:

Private void openfiledialogbtn_click (Object sender, system. eventargs e ){
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 inProgramUsed to execute file operations, write it by yourself, and the source code of this class is attached at the end. 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 restoredirectory. The control dialog box shows whether to restore the current directory addextension and whether to automatically add the default extension checkfileexists checkpathexists before closing.
Before returning to the dialog box, check whether the specified path contains the Container Control and whether the user is prompted when the file is to be created. This parameter is applicable only when validatenames is a true value. Defaultext default extension dereferencelinks
Whether to cancel referencing the first file displayed in the dialog box or the initial directory of the last selected file initialdirector dialog box before returning the shortcut filename from the dialog box to control whether to rewrite the current file prompt user, showHelp is applicable only when validatenames is true. Enabling the "help" button title will display the character validatenames In the title bar of the dialog box. The control dialog box will check whether the file name contains invalid characters or sequences.

2. The savefiledialog event is as follows:

Fileok: the event to be processed when you click "open" or "save". helprequest: the event to be processed when you click "help ".

 

4. Use the following sample code to implement

Private void saveasdialogbtn_click (Object sender, system. eventargs E)
{
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
{
The C #. Net tutorial you are reading is: Visual C # dialog box full contact. /// 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 control whether to display color options allowscriptchange whether to display font Character Set Font whether to display the font in the dialog box allowverticalfonts whether to select the vertical fontmustexist color in the dialog box whether to display the error maxsize when the Font does not exist optional maximum font size minsize min font size scriptsonly display exclude OEM and symbol font showapply show application button showeffects show underline, strikethrough, font color options showHelp Show Help "button

2. fontdialog event

Apply: the event to be processed when you click the "application" button. helprequest: the event to be processed when you click the "help" button.

 

IV. Implementation Code

Private void fontdialogbtn_click (Object sender, system. eventargs E)
{
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 fullopen whether to display the "custom color" section of the "custom color" dialog box first whether the "help" button color display the color anycolor display can be selected whether customcolors of any color displays the custom color "solidcoloronly", and whether only solid colors can be selected

 

III. For the implementation code, see:

Private void colordialogbtn_click (Object sender, system. eventargs E)
{
Colordialog = new colordialog ();
Colordialog. allowfullopen = true;
Colordialog. fullopen = true;
Colordialog. showHelp = true;
Colordialog. Color = color. Black; // initialize the font color in the current text box. When you click "cancel" in the colordialog dialog box
File: // restore the original value
Colordialog. showdialog ();
Richtextbox1.selectioncolor = colordialog. color;
}

It is easy to implement the color dialog box (colordialog). In fact, it is not just a color dialog box, but C # is also very easy to learn. After finishing the colordialog dialog box, let's talk about Printing and page settings.

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.