Author Topic: WinForm UserControl resize issue  (Read 691 times)

0 Members and 1 Guest are viewing this topic.

Jeff_M

  • King Gator
  • Posts: 3998
  • C3D user & customizer
WinForm UserControl resize issue
« on: December 12, 2020, 03:38:37 PM »
I am at wits end here. I have forms using UserControls defined in 2 other projects in my solution. No matter what I have tried, the controls refuse to resize properly when viewed on machines using Windows font scaling other than 100%. Everything else scales fine, only the UserControls I've created over the years fail to do so. All forms & controls are set to AutoScaleMode by DPI. I've set the anchors for the controls, placed in GroupBoxes typically, to be as wanted. I've tried using the Dock option instead of the Anchors. I've tried using both Anchors and Dock together. I've tried using the AutoSize set to Grow&Shrink. All with the same result of not scaling.

Attached are 2 images, one at the standard 100% font scaling, the other at 150%.

Any ideas as to why this is occurring? Please don't tell me to recreate these as WPF windows. I have far too many of them and each requires revised supporting code for saved settings. (I've done this for 2 of my forms, it was not pleasant.)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16842
  • Superior Stupidity at its best
Re: WinForm UserControl resize issue
« Reply #1 on: December 19, 2020, 02:16:12 AM »
My research tells me that there are several problems with autoscale and user controls. I've experienced that in the past.

Some solutions that appear to work in some instances but not all include one or more of the following:
1) AutoScaleMode == font
2) Speaking of AutoScaleMode, if you derive a class from ContainerControl ensure AutoScaleMode == inherit. Setting it to anything else effectively turns off auto scale mode.
3) Do not anchor right or bottom any controls inside a user control, instead, place a panel inside the user control and set dock == fill, then place your controls inside the panel.
4) Do not set any fonts or change the fonts from the default in the designer, instead set fonts in the form constructor or in the show event using the calculated font as a basis.
5) labels with autosize == false will not scale with AutoScaleMode
6) Ensure that all of your containers have the exact same AutoScaleDimensions set, if they are not the same, it will fail. If it is not set on all containers, it will fail.
7) Do not use both Anchor and Dock in the same container. One or the other will take precedence and break the other scaling.
8) If possible, try targeting .net 4.7 .. some of the scaling issues in 4.5.x and 4.6 were addressed.

Of course, the tried and true albeit much more difficult solution is to handle all scaling manually in the offending controls.

Stepping through copious amounts of code to figure out why this doesn't work has shown that when the code is compiled, some form properties are prioritized and therefore happen in the wrong order. AutoScaleDimensions is processed before Font ... therefore, if you set Font in your winform, it will revert the scale to the described font instead of the scaled font.

Consider this:
AutoScaleDimensions is set to 96DPI
Fonts scale to appropriate settings automagically ... lets say from 8pt to 12pt
Redundant Font call in the form constructor, sets the font to 8pt Bold
Head scratching trying to figure out why font is not right.

It's a real pain in the ass trying to resolve this and I've actually said screw it on some projects because quite frankly the effort wasn't worth it.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Jeff_M

  • King Gator
  • Posts: 3998
  • C3D user & customizer
Re: WinForm UserControl resize issue
« Reply #2 on: December 19, 2020, 08:23:25 PM »
Thanks for the response. Keith. I've pretty much found everything you mentioned to be true. The only thing I haven't tried is using a panel set to Dock = Fill in the UserControl. With AutoscaleMode == Font, most of my forms failed to scale at all. Changing this to DPI a few months ago fixed everything except the UserControl issue.

I just edited one of the controls to use a Panel as suggested and tested in the form above. After making a number of adjustments, it appears to have fixed that one control. I edited another, in the exact same way, it is not yet behaving but at least I have hope that it will after a few more tweaks.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16842
  • Superior Stupidity at its best
Re: WinForm UserControl resize issue
« Reply #3 on: December 19, 2020, 11:15:08 PM »
One can always hold out hope ... good luck!
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie