WinForm dialog box, saving, save as, and print controls

Source: Internet
Author: User

Types of dialog boxes to learn:

1. Open File dialog box (OpenFileDialog)

2. Save File dialog box (SaveFileDialog)

3. Font dialog box (fontdialog)

4. Color dialog box (colordialog)

5. Open Folder dialog box FolderBrowserDialog

_______________________________________________________________________________________________________________ ______________

The following program:

Public Form1 () {InitializeComponent ();    //dialog dialog, Session, dialog//showdialog displays the form as a dialog box with the specified owner mode//dialogresult the specified identifier to indicate the return value of the dialog box//floder folder,//browser browser//file file; folder//stream streaming media class private void font Color Toolstripmenui           Tem_click (object sender, EventArgs e) {DialogResult dr= colordialog1.showdialog ();//Display As Palette dialog box if (dr = = DialogResult.OK)//If the user clicked OK to assign the value {textbox1.forecolor = foreground in Colordialog1.color;//textbox1 Color}} private void Select folder Toolstripmenuitem_click (object sender, EventArgs e) {fo Lderbrowserdialog1.showdialog ();//Display as folder browser style TextBox1.Text = folderbrowserdialog1.selectedpath;//The contents of the selected path are displayed in the text        This box//absolute path, relative path} private void Font set Toolstripmenuitem_click (object sender, EventArgs e) {Fontdialog1.showcolor = true;//the color in the Font Settings dialog box FontdIalog1. ShowDialog ();//Open Font settings dialog box Textbox1.font = fontdialog1.font;//text box font setting reference font settings Textbox1.forecolor = Fontd Ialog1. color;//text box color refers to the color in the Font Settings dialog box, the private string lujing;//defines a variable lujing to hold the path to the file store private void Save Stoolstrip Menuitem_click (object sender, EventArgs e) {if (lujing = = "")//if the save path {SA Vefiledialog1.filename = "New file. txt";//Set save name and suffix file type DialogResult dr = Savefiledialog1.showdialog ();//Set up a dialog The box variable Dr receives displays the contents of the Save dialog box if (dr = = DialogResult.OK)//If the user clicks OK {//lujing = Savef iledialog1.filename;//defines a variable to receive the name of the saved file StreamWriter sw = new StreamWriter (savefiledialog1.filename);//Open Open Circulation channel SW. Write (TextBox1.Text);//save Edit Document SW. Close ();//Closed flow path}} else {StreamWriter SW = new Streamwrit ER (lujing);//open Flow channel SW. Write (textbox1.tEXT);//save Edit Document SW.        Close ();//Closed Flow path}} private void new Ntoolstripmenuitem_click (object sender, EventArgs e) {if (this.textbox1.text.length>0)//If Open Notepad has something entered {DialogResult Dr = Message                Box.show ("Whether to save", "Save dialog Box", Messageboxbuttons.yesno); Pop-up whether to save the dialog box if (Dr==dialogresult.ok)//user clicks OK {if (lujing = = "")//If not saved                        path {savefiledialog1.filename = "new file. txt";//Set save name and suffix file type DialogResult DR1 = Savefiledialog1.showdialog ();//Set A Dialog variable Dr receives display the contents of the Save dialog box if (DR1 = = DialogResult .                            OK)//If the user clicks OK {//lujing = savefiledialog1.filename;//defines a variable to receive the name of the saved file                            StreamWriter SW = new StreamWriter (savefiledialog1.filename);//Open stream Channel Sw.              Write (TextBox1.Text);//Save Edit Document              Sw.                        Close ();//Closed flow path}} else { StreamWriter SW = new StreamWriter (lujing);//open Flow channel SW. Write (TextBox1.Text);//save Edit Document SW.             Close ();//Closed Channel}}} lujing = null;//The path is empty because new text does not have a path. This.textBox1.Text = ""; Empty the text box} private void Open Otoolstripmenuitem_click (object sender, EventArgs e) {openfiled Ialog1.            Filter = "Text file |*.txt| all Files |*.*";//filter Open file type DialogResult dr = Openfiledialog1.showdialog ();//Set a dialog box variable to receive                if (dr = = DialogResult.OK)//Click OK button {lujing = openfiledialog1.filename;//assigns the value of the file path to the lujing variable StreamReader sr = new StreamReader (openfiledialog1.filename,unicodeencoding.getencoding ("GB2312"));//Initialize Read stream Media class, and will open the file name, and the current encoding form//equivalent to a water pipe connection two taps, open the switch data for read and write operations               TextBox1.Text = Sr.                ReadToEnd (); Sr.            Close (); }//The second method//openfiledialog ofd = new OpenFileDialog ();//Initializes the class to open the folder, and defines a variable ofd//ofd.            Title = "open"; Ofd. Filter = "text file |*.txt| all Files |*.*";//Set Open File Type dialog box file type, text file txt or all Files//if (ofd.            ShowDialog () = = DialogResult.OK)//If the user clicks the Open File dialog box//{//lujing = Savefiledialog1.filename; FileStream fs = new FileStream (OFD.            FileName, FileMode.Open, FileAccess.Read, FileShare.Read); Invokes a file-based stream class that supports both synchronous operations and asynchronous operations. Assigns the open permission to the variable FS////OFD. FileName get file name and path////filemode.open Open selected File////fileaccess.read set file operation for read/// /fileshare.read allows the subsequent opening of the file to read//StreamReader sr = new StreamReader (FS, encoding.default);//Read the streaming media initialization, the value of FS and when Pre-character encoding assigned to SR//TextBox1.Text = Sr. ReadToEnd ()////TextBox1 receive read///SR.      Close ();//Turn off the read stream media channel      Sr.  Close ();//closes the stream channel with the main file operation//}} private void Save as Atoolstripmenuitem_click (object sender, EventArgs            E) {savefiledialog1.filename = "new as file. txt";//Set save name and suffix file type savefiledialog1.showdialog (); StreamWriter SW = new StreamWriter (savefiledialog1.filename);//open Flow channel SW. Write (TextBox1.Text);//save Edit Document SW. Close ();//closed Circulation channel}

_______________________________________________________________________________________________________________ ______________________________________________
Print Controls:

PrintDocument: Defines an object to be sent to the printer, and the rest of the print controls need to operate on that basis

Use the PrintPage event inside the event

private void Printdocument1_printpage (object sender, System.Drawing.Printing.PrintPageEventArgs e)        {            Font f = New System.Drawing.Font ("Microsoft Jas Black", +);            Brush B = new SolidBrush (color.black);            E.graphics.drawstring (richTextBox1.Text, F, B, 10, 20);
}
_______________________________________________________________________________________________________________ ______________________________________________

PageSetupDialog: Page Setup dialog box

private void Button1_Click (object sender, EventArgs e)        {            pagesetupdialog1.document = printDocument1;            Pagesetupdialog1.showdialog ();        }

_______________________________________________________________________________________________________________ ___________

PrintPreviewControl: Print Preview (contains only part of the document, and you need to allocate space on the form)

private void Button2_Click (object sender, EventArgs e)        {            printpreviewcontrol1.document = printDocument1;        }
_______________________________________________________________________________________________________________ ______________________________________________

PrintPreviewDialog: Print Preview dialog box

private void Button2_Click (object sender, EventArgs e)        {            printpreviewdialog1.document = printDocument1;            Printpreviewdialog1.showdialog ();        }
_______________________________________________________________________________________________________________ ______________________________________________

PrintDialog: Print dialog box (Note that you need to receive a return value of OK when printing is performed)

private void Button3_Click (object sender, EventArgs e)        {            printdialog1.document = printDocument1;            DialogResult dr = Printdialog1.showdialog ();            if (dr = = DialogResult.OK)            {                printdocument1.print ();            }
The above content can be used to do some kind of notepad-like application, pending update



WinForm dialog box, saving, save as, and print controls

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.