Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoScaleMode problems with changed default font

Tags:

c#

.net

winforms

I finally found an answer for my question. In short, the effect does not arise when one sets the font of each control individually instead of setting the font of the containing form. This way, the auto scaling feature works as as it should. Interestingly, setting the font of the controls changes the auto-scaling behaviour even if the AutoScaleMode property is set to AutoScaleMode.Dpi, not only when it is set to AutoScaleMode.Font.

As a pragmatic solution, we created a small command line program, which reads the designer.cs files, scans if all controls have an explicit font assignment, and if not, adds the assignment into a newly created copy of the designer code. We embedded this program into our automatic test suite, so whenever a form gets new controls, or a new form is added, and the dev forgets to add the explicit font assignment, the tests will fail. In between, we have this solution working from the time I asked this question first (4 years ago), and it saved us from scaling problems for several times since then.


Had this frustrating problem causing the spacing on the form to get bewildering out of proportion, however, changed the AutoScaleMode to "None" and the problem completely disappeared.


Keep in mind that since many of the issues mentioned are related to font size, it is important to be consistent with the font family and size. This means set the font on the base form or base usercontrol (if you have one) and let the controls inherit that setting. I noticed than when I have a form with a UserControl inside it, if I selected the control and changed the font size, some items resized and some didn't. I realized that the items that didn't resize had their font setting specifically set (over-ridden). It was at that time that I realized what it meant when properties were highlighted in bold. So for example if you have a label and the font prop is bolded that means someone changed it. You should clear all those font props that were set that way so that they get their font from the parent, in this case the containing form. You can simply highlight the font property text and delete or right click the font prop and select clear. That will remove the font line from the designer file and allow the control to inherit the font from its parent. This will probably jump back to Microsoft Sans Serif but if you build it will pick up the font from its parent Of course you should follow proper design using layout panels and anchor and dock properties but I find that in general if you clear all the over-ridden font props you will find that if you select the user control from within your form and change the autoscalemode to None, you will have better luck Also for testing, if you then change the font size for the userontrol all the controls in it should resize (as long as no font props are over-ridden) And as long as the form is designed using the proper layout panels, everything should display fine on other resolutions. At least so far for me this has worked.


I was able to resolve a similar issue with compact framework 3.5 on VS 2008. In my case, I have a tabcontrol, and each tabpage has a Panel, and all are docked as full to their parents. Each panel contains several label and textbox controls, so the idea is that when the user opens the SIP (soft input panel/keyboard) that the scrollbar will appear on the right and the textbox controls would scale in width to avoid painting an additional horizontal scrollbar.

My initial attempt had the forms' autoscale mode set to dpi, each tabpages' autoscroll property set to true, and each panels' autoscroll property set to true. Each label was anchored to top,left, and each textbox control was anchored to left,top,right. The forms were created in the designer with a screen width of 240 pixels, and when run on a vga device with a 480 pixel screen width, the textboxes would be painted with space on the right sufficient for 2 scrollbars (presumably one for the tabpage and one for the panel) even though the scrollbars were not appearing. When activating the SIP, the behavior was correct, in that all of the textboxes resized, but I still had 40 or so pixels of dead space between the right side of the textbox and the scrollbar.

I was able to resolve the issue simply by setting the panels' autoscroll property to false, then setting then to true a run time when the SIP was activated. This had the desired result of autoscaling to the full width of the screen in either pixel width, and dynamically resizing the textbox controls as the scrollbar is activated or de-activated.

As a side note, the compact framework (3.5) does not have the Font autoscale mode (only none, dpi, and inherit), but I tried resetting the font of each textbox control as the original author suggested, but this didn't have any effect on the controls' autoscaling.