页面设置不断被意外修改
看来您可以保留设置文件 PrinterSettings 和 PageSettings,它们是可序列化的等。太棒了!一切都很顺利,直到我尝试保留我的页面设置和边距。每次我更改边距时,保存设置,然后将它们重新加载回 PageSetupDialog.PageSettings - 它们已被修改。有谁知道为什么会发生这种情况?这是驱动程序或 Windows 正在执行的操作吗?它很奇怪,见下文:
这里我显示了对话框:
PageSetupDialog psd = new PageSetupDialog();
psd.PageSettings = MySettings.Default.pageSettings;
if (psd.ShowDialog() == DialogResult.OK)
我手动将值更改为 5。
然后我保存更改:
MySettings.Default.pageSettings = psd.PageSettings;
当我重新加载对话框并从设置文件加载设置时,值已更改为 2!?
更新:
因此,只是尝试扩展问题 - 如果我们显示 PageSetupDialog第一次使用默认设置值,所有边距均为 10。如果我们在调试期间检查这些值,那么我们会看到它们的表示方式如下:
所以它们都是 100,很奇怪,但我预计它们是 10,即像对话框中所说的 10mm,所以我不知道 100 属于什么度量单位,但无论如何我们假设它与对话框中的 10 相关。
然后我们在对话框中将它们全部编辑为 5 并按“确定” - 这样我们就可以通过此代码:
if (psd.ShowDialog() == DialogResult.OK)
因此我们希望在检查 pageSettingDialog 对象时看到边距的某些值全部为 5(或 50,看起来像这样) 10 与 100 的混淆就是这种情况,见上文)。让我们看一下该对象:
低,看它不是我们所期望的,它是 20 一些奇怪的原因。这就是我陷入困境的地方,我只是无法弄清楚这里所看到的背后发生了什么。为什么它从 50 变成 20?为什么它不保留我设置的值? 希望这能更好地解释它。
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 that 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)
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!?
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:
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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
页边距的值以百分之一英寸为单位,默认值为 100(= 1 英寸)。 5 毫米等于 0.196845455 英寸,四舍五入为 0.20。 0.20 乘以 100 等于 20。请参阅 MSDN 上的这篇文章.
这解释了您在调试会话期间所看到的内容。至于你们的对话,我认为存在一些文化问题。如果有时间的话,我会进一步研究这个问题,但现在我希望这能对你有进一步的帮助。
编辑
好的,我认为发生的情况是对话框在离开对话框时转换指标,但在打开时不会转换。我认为您可以通过在打开对话框时转换您的值来创建解决方法。像这样的事情:
您的 PageSetupDialog 还有一个 EnableMetric 属性,这也应该可以解决您的问题。不过我还没有测试过。请参阅此处。
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:
Your PageSetupDialog also has an EnableMetric property, which should also solve your problems. I have not tested this though. See here.
由于我们没有更多的代码可供查看,因此我将给出我的“最佳猜测”。
我的想法是,当对话框打开时,您的代码将值设置为 2。设置保存为 5,但由于硬编码设置,对话框显示 2。确保没有任何内容覆盖对话框显示中设置的读取。
或者,在保存表单时,有一些内容会覆盖该值。
查看您的设置读取代码和设置写入代码,看看代码中是否有硬编码的“2”。
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.