PageSetupDialog Inches/Millimeters

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

The PageSetupDialog seems to have a bug with the PageSettings.Margins property
Whenever I call the PageSetupDialog, hit OK and then show the PageSetupDialog again, it changes the margins (10 mm becomes 3.9 mm). Obviously this has something to do with millimeters to inches conversion (10 / 2.54 = 3.937). Somewhere I read that this was a known bug in VS.NET 2002 and that it was fixed by VS.NET 2003 (see http://dotnet247.com/247reference/msgs/41/208791.aspx). However, I'm using VS.NET 2003 and I still have the problem..

How can I fix this
Guid
 
Hi Guido,

I can reproduce this behavior on my system, it is possibly a bug , I have
forwarded it to the product group to let them investigate it.
We can workaround this problem by converting the margin to
HundredthsOfMillimeter manually, after showing the PageSetupdialog like:
<code>
pageSetupDialog1.ShowDialog();
pageSetupDialog1.PageSettings.Margins =
PrinterUnitConvert.Convert(pageSetupDialog1.PageSettings.Margins,PrinterUnit
..ThousandthsOfAnInch,PrinterUnit.HundredthsOfAMillimeter);
</code>

Does it work in your scenario?

Please feel free to let me know if you still have problem on this issue.


Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Hi Ying-Shen,

the workaround has the following problems:
1) rounding errors
2) it does not work if you connect the PageSetupDialog to the PrintDocument.

To better understand my problems, here is how you can set up a simple test
application:
- create a new Windows Forms application (C#)
- use the visual designer to drag the following components to Form1:
PrintDocument, PageSetupDialog, PrintPreviewDialog
- set the Document property of pageSetupDialog1 and printPreviewDialog1 to
printDocument1
- add an event handler for the PrintPage event of printDocument1
- add the following code to the PrintPage event handler:
e.Graphics.DrawRectangle(
new System.Drawing.Pen(Color.Black),
e.MarginBounds
);
- add two Buttons to Form1
- add the following code to the Click event of Button1:
this.pageSetupDialog1.ShowDialog();
- add the following code to the Click event of Button2:
printPreviewDialog1.ShowDialog();

Now you can start the application and do some tests:
1) Click on Button1 and enter some margins in millimeters (e.g. 10mm for
each side). Click OK and then on Button2 and it works as expected. However,
if you go back to the PageSetupDialog, you'll see other settings than 10mm.

2) Add your workaround code in the event handler of Button1:
pageSetupDialog1.PageSettings.Margins =
PrinterUnitConvert.Convert(pageSetupDialog1.PageSettings.Margins,PrinterUnit
..ThousandthsOfAnInch,PrinterUnit.HundredthsOfAMillimeter);
Now the PageSetupDialog correctly remembers the margins (except for rounding
errors). However, the print preview uses the converted margins (if you enter
10 mm you get 1 inch).

Guido
 
Hi Guido,

Do you mean if you open the PageSetupDialog and click cancel then open it
again, the margin was changed to 25.4 mm?

After a second look at the code, the margin will not be changed when we
click cancel button, so we should only do the conversion when OK button
was clicked. So here is the modified version of the snippet:
<code>
if (pageSetupDialog1.ShowDialog() == DialogResult.OK)
{

System.Diagnostics.Debug.WriteLine("Margin",printDocument1.DefaultPageSettin
gs.Margins.ToString());
pageSetupDialog1.PageSettings.Margins =

PrinterUnitConvert.Convert(pageSetupDialog1.PageSettings.Margins,PrinterUnit
.ThousandthsOfAnInch,PrinterUnit.HundredthsOfAMillimeter);
}
</code>

Let me know if the problem still exists.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Back
Top