質問

I have this problem that I can't work out: I have a FlowLayoutPanel with a width of 240; I have created a custom control with the same width and I add it programmatically to my panel. I expect it to fit perfectly, but it doesn't. The controls that I add looks much bigger than the panel.

The funny thing is that I have checked the width of both the panel and the controls during debug and they are still 240...

Any idea? Thanks in advance!

役に立ちましたか?

解決

A UserControl, like a Form, remembers where it came from. The diagnostic here is that it was designed on a machine that had its video adapter's dots-per-inch setting at a higher value than the machine on which you try to use it. So Winforms compensates for the difference and rescales the control to match its original design size in inches. So the control's Size property in pixels gets larger by the DPI ratio.

You can diagnose this by looking at the Designer.cs file for the UserControl. Scroll towards the bottom of the InitializeComponent() method until you see:

  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

The AutoScaleDimensions property value is doing this remembering. The values as shown are relevant for a machine that runs at 96 DPI. Compare it to the same property assignment in your form's Designer.cs file, with the expectation that there will be a mismatch.

Just editing the value in the user control's Designer.cs file to make it match the form's value is a workaround. You do however have to check that images still line up correctly with other controls and that the fonts still fit the controls. They won't unless you kept the default setting so they inherit from the parent. Easy to see that going wrong. In general, stay out of trouble by keeping the machines you use at the same setting.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top