views:

338

answers:

2

I'm trying to set up the print page for a document, using PageSetupDialog.

Before I open the dialog, the document is set correctly, page size and page source are set correctly too. But when I return from the dialog after select a different paper size and paper source, the paper size is not correctly reflected, while the paper source is fine. Yes, I am pressing OK button.

This issue is not new but so far there has been no proper answer.

    PageSetupDialog dlgPageSetup = new PageSetupDialog();
    dlgPageSetup.Document = this.printDocument1; //this is fine, assume that.
    dlgPageSetup.PageSettings.PaperSize = new PaperSize("My Custom", 1012, 800);
    dlgPageSetup.PageSettings.PaperSource.SourceName = "Envelope";
    if (dlgPageSetup.ShowDialog(this) == DialogResult.OK) {
        System.Diagnostics.Trace.WriteLine("DEBUG: "
              + dlgPageSetup.PageSettings.PaperSize);
        System.Diagnostics.Trace.WriteLine("DEBUG: "
              + dlgPageSetup.PageSettings.PaperSource);
    }

I'm using .Net 2.0, VS 2k5.

Link to original issue.

I am guessing this still is a bug, and its related to custom page size. Has anybody got solution for this problem?

A: 

I worked around the problem by:

  1. setting the Document property on the pagesettings dialog to (none)
  2. recreating the print document if I see that it is set to a custom PageKind

So before I open the printsettings dialog, I check the PageKind of the print document, recreate if necessary, and then open the dialog.

if(printDocument1->DefaultPageSettings->PaperSize->Kind == 
    System::Drawing::Printing::PaperKind::Custom)
{
  RecreatePrintDocument();
}
pageSetupDialog1->PageSettings = printDocument1->DefaultPageSettings;
pageSetupDialog1->PrinterSettings = printDocument1->PrinterSettings;
Windows::Forms::DialogResult dresult = pageSetupDialog1->ShowDialog();

In RecreatePrintDocument(), I create a new printdocument and assign the handler, that sort of thing.

This isn't a great solution, because we just forget the page settings if the user chooses a custom page kind, but it's something to start with.

Matthew Lowe
Thanks Matthew! I'll try and post the result in few hours. Thanks for replying to this question! =)
Nayan
Matthew, this probably will not work, or I am not able to see what you're trying to depict here. Have a look at my code http://social.msdn.microsoft.com/forums/en-US/winforms/thread/81bb2cea-8d47-4ddc-a174-14d6bc196de7/ Check the last post by me.
Nayan
You're right, this will not fix the problem you reproduce in that last post. If I understand correctly, you're trying to programmatically set the page settings dialog to a custom paper size, and as I'm sure you know this breaks the dialog (for all subsequent paper sizes also). What I do above is make sure that I /don't ever/ programmatically set the page settings dialog to a custom paper size (it's ok if the user does it). This means that the dialog appears to lose its settings, but at least it doesn't break for subsequent selections. Have you tested this in .NET 4 to see if it's still broken?
Matthew Lowe
A: 

This is a known bug. Till .NET 3.5, it is still reproducible. More details on thread http://social.msdn.microsoft.com/forums/en-US/winforms/thread/81bb2cea-8d47-4ddc-a174-14d6bc196de7/

Nayan