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

Source: Internet
Author: User
Tags filter count
Printing | controls

Open it:

private void Openfiledialogbtn_click (object sender, System.EventArgs e) {
OpenFileDialog openfiledialog=new OpenFileDialog ();
openfiledialog.initialdirectory= "c:\\"; note that when you write the path here, you use c:\\ instead of c: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 ("");
} }

Save:

private void Saveasdialogbtn_click (object 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://saved a file with a name
Fsaveas.writefile (richTextBox1.Text);
} }
}

Change font Size

private void Fontdialogbtn_click (object 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;//Changes the currently selected text to a font
} }

Change the font color:

private void Colordialogbtn_click (object sender, System.EventArgs e)
{ColorDialog colordialog=new colordialog ();
Colordialog.allowfullopen=true;
Colordialog.fullopen=true;
Colordialog.showhelp=true;
colordialog.color=color.black;//initializes the font color in the current text box, and when the user clicks the Cancel button 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 line number of the page
float ypos=0;//The portrait position of the printed string
int count=0;//line 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 does not implement a different color printing
SolidBrush b =new SolidBrush (CLR);//Brushes
fnt= richtextbox1.selectionfont;//the current print font
LINESPERPAGE=E.MARGINBOUNDS.HEIGHT/FNT. GetHeight (e.graphics); The number of rows to print per page
file://one page line by line
while (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, there are still unfinished pages, and the next print event is issued,
file://in the next print Linereader automatically reads the last item that was not printed. Linereader can record the current read position
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 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.