Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
On button click event in windows form I use following code to show page setup dialog.
C#
PageSetupDialog pageSetup = new PageSetupDialog();
pageSetup.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
pageSetup.PageSettings = new System.Drawing.Printing.PageSettings();
pageSetup.EnableMetric = false;
pageSetup.ShowDialog();


After setting page setup and printer I clicked Ok. But when page setup dialog opens again it is reseted to the default settings. Previously changed values not displayed.
Posted
Updated 4-Jul-18 1:04am

You are NOT saving any settings. There basically are two ways to get previous values as preset defaults:

1. keep using the same dialog, i.e. move the new PageSetupDialog(); line out of the loop.

2. or actually do save the settings, then restore them to your next dialog when you need it.

Obviously #1 is the easier way.

:)
 
Share this answer
 
Of course it would do that because you create a brand new instance of the dialog using
C#
PageSetupDialog pageSetup = new PageSetupDialog();
What else could you expect?

You really need to re-use the same object over and over. The best and most universal way to do that on-demand using lazy initialization pattern, see http://en.wikipedia.org/wiki/Lazy_initialization[^].

I would simply keep the reference to the dialog as a field of the main form, so the whole picture would look like this:

C#
public class MainForm : Form {

    PageSetupDialog PageSetupDialog {
        get {
            if (fPageSetupDialog == null) {
                fPageSetupDialog = new PageSetupDialog();
                fPageSetupDialog.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
                fPageSetupDialog.PageSettings = new System.Drawing.Printing.PageSettings();
                fPageSetupDialog.EnableMetric = false;
            }
            return fPageSetupDialog;
    } //PageSetupDialog

    PageSetupDialog fPageSetupDialog; //null at first, don't use it directly

    //...

    void SomeMethodWherePageSetupDialogIsUsed() {
        // of the very first call, an instance will be created
        // on other calls, same instance will be used
        // preserving all the previous settings
        PageSetupDialog.ShowDialog();
        //...
    } //SomeMethodWherePageSetupDialogIsUsed

} //class MainForm


—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900