Figure 1
Creating a PrintDialog
We can create a PrintDialog at design-time as well as at run-time.
Design-time
To
create a PrintDialog control at design-time, you simply drag and drop a
PrintDialog control from Toolbox to a Form in Visual Studio. After you
drag and drop a PrintDialog on a Form, the PrintDialog looks like Figure
2.
Creating
a PrintDialog control at run-time is simple. First step is to create an
instance of PrintDialog class and then call the ShowDialog method. The
following code snippet creates a PrintDialog control.
PrintDialog PrintDialog1 = new PrintDialog();
PrintDialog1.ShowDialog();
Printing Documents
PrintDocument
object represents a document to be printed. Once a PrintDocument is
created, we can set the Document property of PrintDialog as this
document. After that we can also set other properties. The following code snippet creates a PrintDialog and sends some text to a printer.
private void PrintButton_Click(object sender, EventArgs e)
{
PrintDialog printDlg = new PrintDialog();
PrintDocument printDoc = new PrintDocument();
printDoc.DocumentName = "Print Document";
printDlg.Document = printDoc;
printDlg.AllowSelection = true;
printDlg.AllowSomePages = true;
if (printDlg.ShowDialog() == DialogResult.OK)
printDoc.Print();
}
No comments:
Post a Comment