C # Code dialog control implementation (open, save, change font, color, print function)

Source: Internet
Author: User

Open:

Private void openfiledialogbtn_click (Object sender, system. eventargs e ){
Openfiledialog = new openfiledialog ();
Openfiledialog. initialdirectory = "C :\\"; // note that c: \ instead of C: openfiledialog is used to write the path here. 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 ("");
}}

Save:

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 );
}}
}

Change font size

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.
}}

Change font color:

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;
}

Print:

private void printdocument_printpage (Object sender, system. drawing. printing. printpageeventargs e)
{float linesperpage = 0; // The row number of the page
float ypos = 0; // print the vertical position of the string
int COUNT = 0; // row counter
float leftmargin = E. marginbounds. left; // left margin
float topmargin = E. marginbounds. top; // top margin
string line = NULL; // line string
color CLR = richtextbox1.selectioncolor; // The current print color, in my Program Print in different colors
solidbrush B = new solidbrush (CLR); // brush
fnt = richtextbox1.selectionfont; // The current print font
linesperpage = E. marginbounds. height/fnt. getheight (E. graphics); // Number of printable lines per page
file: // print a page row by row
while (count {
ypos = topmargin + (count * fnt. getheight (E. graphics);
E. graphics. drawstring (line, fnt, B, leftmargin, ypos, new stringformat ();
count ++;
}file: // If the page is printed and the line is not empty, the next printing event is triggered.
File: // In the next print, linereader will automatically read the last unprinted content. Linereader can record the currently read location
If (line! = NULL)
E. hasmorepages = true;
else
E. hasmorepages = false;
}

Private void printpreviewbtn_click (Object sender, system. eventargs E)
{Linereader = new stringreader (richtextbox1.text );
Try
{Printpreviewdialog printpreviewdialog1 = new printpreviewdialog ();
Printpreviewdialog1.document = printdocument;
Printpreviewdialog1.formborderstyle = formborderstyle. fixed3d;
Printpreviewdialog1.showdialog (this );
} Catch (exception excep)
{MessageBox. Show (excep. message, "print error", messageboxbuttons. OK, messageboxicon. Error );
Return;
}}

Private void printdialogbtn_click (Object sender, system. eventargs E)
{Printdialog = new printdialog ();
Printdialog. Document = printdocument;
If (printdialog. showdialog ()! = Dialogresult. Cancel)
{Try
{Printdocument. Print ();
} Catch (exception ex)
{MessageBox. Show (ex. Message );
}}
}

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.