“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.
C# Programming Series
C# Common Dialog Boxes for Printing
Yashavant Kanetkar
Dialog Boxes for Printing
For these dialog boxes .NET has provided the PrintPreviewDialog control, the PrintDialog control and the PageSetupDialog control. To illustrate the use of these dialog boxes let us create a Windows Form application called prntdlgs. We also added a vastly used OpenFileDialog control to the form along with two textboxes-filename and mytext-and five buttons-a 'Browse' button named browse, an 'Open' button named open, a 'PageSetup' button named pgsetup, a 'Print Preview' button named preview, and a 'Print' button named print. Next we added five components to the component tray and changed their names as shown below:
| Component |
Name |
|---|---|
| PrintDocument | mypdoc |
| PageSetupDialog | mypgsdlg |
| PrintPreviewDialog | mypreviewdlg |
| PrintDialog | myprintdlg |
| OpenFileDilaog | myopendlg |
The UI of the prntdlg application is shown in the following figure.
Next we have added Click event handlers for all the five buttons. The handler for the browse button is given below.
private void browse_Click ( object sender, System.EventArgs e )
{
myopendlg.Filter = "Text Files ( *.txt ) | *.txt" ;
myopendlg.ShowDialog( ) ;
if (myopendlg.FileName != "" )
filename.Text = myopendlg.FileName ;
}
The 'Browse' button would let the user to select a file for printing. The user would select the file from standard Open File dialog box. We have displayed the dialog box in this handler. The selected file name is displayed in the text box. As soon as the user clicks the 'Open' button the following handler gets called.
private void open_Click ( object sender, System.EventArgs e )
{
StreamReader reader = new StreamReader ( filename.Text ) ;
mytext.Text = reader.ReadToEnd( ) ;
reader.Close( ) ;
}
Here we have read the file contents with the help of an object of the StreamReader class and displayed them on the mytext textbox. Now if the user wants to change the page settings he needs to click the 'Page Settings' button. On doing so the following handler would get called.
private void pgsetup_Click ( object sender, System.EventArgs e )
{
try
{
mypgsdlg.PageSettings = ps ;
mypgsdlg.ShowDialog( ) ;
}
catch ( Exception ex )
{
MessageBox.Show ( ex.Message ) ;
}
}
We have added an object referred to by ps of the PageSettings class as a data member of the form class as follows:
PageSettings ps = new PageSettings( ) ;
The zero-argument constructor of this class creates an object of the PageSetting class for the default printer. The default constructor initializes all fields to their default values. Next we have set the PageSettings property of the PageSetupDialog class to ps. Then we have displayed the Page Setup Dialog box using the ShowDialog( ) method.
The user can also preview the page to be printed by clicking on the 'Print Preview' button. On doing so the following handler would get called.
private void preview_Click ( object sender, System.EventArgs e )
{
try
{
str = mytext.Text ;
mypdoc.DefaultPageSettings = ps ;
mypreviewdlg.Document = mypdoc ;
mypreviewdlg.ShowDialog( ) ;
}
catch ( Exception ex )
{
MessageBox.Show ( ex.Message ) ;
}
} Here first we have collected the string in mytext textbox in str. We have added str of type string as a data member of the form class. Next we set the DefaultPageSettings property of the PrintDocument class to ps. Then we set the Document property of the PrintPreviewDialog class to mypdoc, which specifies the document to be previewed. Next we have displayed the dialog using the ShowDialog( ) method.
On clicking the 'Print' button the following handler gets called.
private void print_Click ( object sender, System.EventArgs e )
{
try
{
mypdoc.DefaultPageSettings = ps ;
str = mytext.Text ;
myprintdlg.Document = mypdoc ;
if ( myprintdlg.ShowDialog( ) == DialogResult.OK )
mypdoc.Print( ) ;
}
catch ( Exception ex )
{
MessageBox.Show ( ex.Message ) ;
}
}
Here first we have set the DefaultPageSettings property of the PrintDocument class to ps. Next we have collected the text of mytext textbox, which is to be printed in str. The document to be printed is then set to the Document property of the PrintDialog class. Next we displayed the PrintDialog box using the ShowDialog( ) method and if the result happens to be DialogResult.OK, i.e if the user clicks OK, we have called the Print( ) method of the PrintDocument class.
On calling the Print( ) method the following handlers get invoked.
private void mypdoc_BeginPrint ( object sender, System.Drawing.Printing.PrintEventArgs e )
{
f = new Font ( "Arial", 12 ) ;
b = new SolidBrush ( Color.Black ) ;
strformat.Trimming = StringTrimming.Word ;
}
private void mypdoc_PrintPage ( object sender, System.Drawing.Printing.PrintPageEventArgs e )
{
RectangleF myrect = new RectangleF( e.MarginBounds.Left,
e.MarginBounds.Top, e.MarginBounds.Width,
e.MarginBounds.Height ) ;
SizeF sz = new SizeF ( e.MarginBounds.Width, e.MarginBounds.Height - f.GetHeight(e.Graphics ) ) ;
e.Graphics.MeasureString ( str, f, sz, strformat, out chars, out lines ) ;
printstr = str.Substring ( 0, chars ) ;
e.Graphics.DrawString ( printstr, f, b, myrect, strformat ) ;
if ( str.Length > chars )
{
str = str.Substring ( chars ) ;
e.HasMorePages = true ;
}
else
e.HasMorePages = false ;
}
The code written in this handler takes care of multiple pages being printed as well as word wrapping.



