开发者

PageSettings keep getting modifed unexpectedly

开发者 https://www.devze.com 2023-01-26 21:38 出处:网络
It seems that you can persist in a settings file PrinterSettings and also PageSettings, they are Serializable etc.Great!Everything was going OK until I tried to persist my PageSettings and the margins

It seems that you can persist in a settings file PrinterSettings and also PageSettings, they are Serializable etc. Great! Everything was going OK until I tried to persist my PageSettings and the margins. Everytime I change the margins, save the settings and then reload them back into the PageSetupDialog.PageSettings - they have been modified. Does anyone know why this happens? Is it something th开发者_如何学运维at the driver or Windows is doing ? Its bizarre, see below:

Here I show the dialog:

PageSetupDialog psd = new PageSetupDialog();
psd.PageSettings = MySettings.Default.pageSettings;

    if (psd.ShowDialog() == DialogResult.OK)

PageSettings keep getting modifed unexpectedly

I manually change the values to 5.

Then I save the changes:

MySettings.Default.pageSettings = psd.PageSettings;

When I reload the dialog and load in the settings from the settings file the values have changed to 2!?

PageSettings keep getting modifed unexpectedly

UPDATE:

So just to try and expand on the problem - if we show the PageSetupDialog for the first time with default settings values which are 10 for all margins. If we inspect the values during debug then we see them represented like this:

PageSettings keep getting modifed unexpectedly

So they are all 100, strange but I expected them to be 10 i.e. like 10mm like the dialog says, so I don't know what unit of measure 100 is pertaining to be but anyway lets assume that it correlates with 10 in the dialog.

We then edit them all to 5 in the dialog and press OK - so we get past this code:

if (psd.ShowDialog() == DialogResult.OK)

so we would expect to see on inspection of the pageSettingDialog object to see some values for the margins to be all 5 (or 50, as seems to be the case in the 10 vs 100 confusion, see above). Let's have a look at the object then:

PageSettings keep getting modifed unexpectedly

Low and behold it's not what we were expecting, it's 20 for some bizarre reason. That's where I get stuck, I just can't work out whats going on behind the seens here. Why is it changing from 50 to 20?, why won't it keep the value I set? Hope that explains it a little bit better.


The values for margins are in hundredths of an inch, with a default value of 100 (= 1 inch). 5 mm equals 0.196845455 inch, which rounds to 0.20. 0.20 times 100 equals 20. See this article on MSDN.

This explains what you are seeing during your debug session. As for your dialog, I think there is some cultural issue going on. I will look into this more if I have some time, but for now I hope this will help you a little further.

EDIT

Ok, what I think happens is that the dialog converts the metrics when leaving the dialog but doesn't do it when opening. I think you can create a workaround by converting your values when opening the dialog. Something like this:

PageSetupDialog psd = new PageSetupDialog();
Margins currentMargins =  MySettings.Default.pageSettings.Margins;
psd.PageSettings.Margins = new Margins(
   ConvInchToMm(currentMargins.Left), ConvInchToMm(currentMargins.Right), 
   ConvInchToMm(currentMargins.Top), ConvInchToMm(currentMargins.Bottom));

private int ConvInchToMm(int hundrInch) 
{
   return (int)(hundrInch * 2.54);
}

Your PageSetupDialog also has an EnableMetric property, which should also solve your problems. I have not tested this though. See here.

PageSetupDialog psd = new PageSetupDialog();
psd.EnableMetric = true;


Since we don't have any more code to look at, I'm going to give it my "best guess".

My thought is that your code is setting the value of 2 when the dialog is opening. The settings are saved as 5 but dialog is displaying 2 because of a hardcoded setting. Make sure that there is nothing overriding the reading of the setting in the dialog display.

Alternatively, upon saving the form, there is something overriding the value.

Look in your settings read code and your settings write code to see if there is a "2" anywhere hardcoded in your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消