Click here to Skip to main content
6,635,160 members and growing! (17,376 online)
Email Password   helpLost your password?
Desktop Development » Menus » Toolbars     Intermediate

Print Preview Control Bar

By MikePEQ

A toolbar for controlling print previews.
C#, Windows, .NET 2.0VS2005, Dev
Posted:2 May 2006
Views:50,298
Bookmarked:59 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 3.83 Rating: 4.54 out of 5

1

2

3
4 votes, 57.1%
4
3 votes, 42.9%
5

Print Bar Screenshot

Introduction

I recently developed an application with quite a few standard reports. The reports all have a set selection criteria, and didn't need filtering, so no front end was necessary - just straight to the report.

I tried the PrintPreviewDialog control, but it doesn't have a way to set the print properties (margins, orientation, printer etc.). I also have a form management component (included in the project) which automatically restores a form to its previous state (window state, position, size). I couldn't use this with a PrintPreviewDialog control.

I, therefore, needed a common method to replicate the PrintPreviewDialog control - but adding in the missing functionality. So the ucPrintBar was born.

Using the code

There is an example form included in the source which should hopefully expand and illustrate these instructions.

The ucPrintBar is an extension of the toolstrip - with all the controls pre-added. To use it on a form, just add it to the form and you should see the toolstrip shown above. You will also need to add a PrintDocument and a ucFormManagement component. The PrintDocument is obvious, but the ucFormManagement component is used to automatically save and restore print properties (this is explained further later).

In the properties of the ucPrintBar, you will need to assign the Document property and the FormManagement property as seen below. The PreviewControl is unassigned at design time because it is assigned dynamically at runtime.

Print Bar usage Screenshot

You will need a class variable for the PreviewControl:

private System.Windows.Forms.PrintPreviewControl preview

For the ucPrintBar, you will need to create a PrintPropertiesChanged event:

this.ucPrintBar1.PrintPropertiesChanged += 
    new printPropertiesChanged(this.ucPrintBar1_PrintPropertiesChanged);

with the following functions:

private void ucPrintBar1_PrintPropertiesChanged()
{
    //If the the print properties change (orientation, margins etc)

    // we need to re-create the report with the new parameters. 

    this.Controls.Remove(this.preview);
    this.setupReport();
}

private void setupReport()
{
    this.preview = new System.Windows.Forms.PrintPreviewControl();
    this.preview.Document = this.printDocument1;
    this.preview.Dock = System.Windows.Forms.DockStyle.Fill;
    this.preview.Location = new System.Drawing.Point(0, 25);
    this.preview.Name = "preview";
    this.ucPrintBar1.PreviewControl = this.preview;
    this.Controls.Add(this.preview);
    this.ucPrintBar1.loadDefaults();
}

The setupReport function is also called from the form Load event.

The thing I had most difficulty with was refreshing the print preview after changing the print properties. The obvious things didn't work (calling PreviewControl.Refresh, repainting the screen etc.), so in the end, I resorted to removing the PreviewControl and recreating it in the PrintPropertiesChanged event handler.

There you have it - all you have to do now is create the report itself.

A note on the ucFormManagement component

I like to have my forms open up with the same size and position as when I last used it. It irritates me when I have to resize a form every time I use it (and I know quite a few other people who think the same way).

So, all my applications use my ucFormManagement component. All you have to do is drop the component on a form, and it automatically saves the five form state variables (WindowState, Top, Left, Height, Width) to the registry. When the form is opened, the component restores the last values.

That's the automatic stuff. I also have the ability to manually store the form information if I need to. The component has a SortedList<string, object> for additional non-standard information. I used this extra facility in the ucPrintBar to store the print properties (Left Margin, Right Margin, Top Margin, Bottom Margin, Orientation). If you don't want to store the print properties between uses, just don't assign the FormManagement property of the ucPrintBar. Here's an example of how to add some additional form information:

this.ucFormManagement1.addExtra("LeftMargin", 
     this.document.DefaultPageSettings.Margins.Left);

Here's an example of how to retrieve the additional form information:

if (this.ucFormManagement1.Extras != null)
{
    if (this.ucFormManagement1.Extras.ContainsKey("LeftMargin"))
    {
        leftMargin = 
          Convert.ToInt32(this.ucFormManagement1.Extras["LeftMargin"]);
    }
}

History

  • 2nd May 06: Initial version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

MikePEQ


Member

Occupation: Web Developer
Location: New Zealand New Zealand

Other popular Menus articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
Generalpage count Pinmemberdenistoto21:19 26 Feb '08  
QuestionPrinting : Page 1 to 5 Pinmemberanishmm1:55 13 Nov '07  
Generalvb.net??? work? [modified] PinmemberCristiano F. Moura6:29 29 Jun '06  
AnswerRe: vb.net??? work? PinmemberMikePEQ9:21 29 Jun '06  
GeneralGood Article! PinmemberMahesh Sapre3:13 6 May '06  
GeneralRe: Good Article! PinmemberMikePEQ9:22 29 Jun '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 May 2006
Editor: Smitha Vijayan
Copyright 2006 by MikePEQ
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project