Yesterday we tackled a strange problem at work. Some of our application forms that were displayed correctly on our development machine and on most of our test machine, failed to load on a machine with a Japanese Windows OS. A NullReferneceException was thrown during their load.
For those of you who don’t know what is AutoScaleMode let me explain shortly. This is a property of ContainerControl that specifies how to perform automatic scaling of the control and its’ children. Automatic scaling is a feature that lets you develop a form or control on one screen resolution or font settings and use it on a machine with other settings. The form and its’ children will intelligently resize if you use it correctly. If you want more information about Automatic Scaling try this article Automatic Scaling in Windows Forms.
Now we are ready to show the problem. Let’s look at an example.
There is a BaseControl class that is defined as follows:
And here is the code for some DerivedControl class:
Since we developed our application on a machine with English settings and most of our test machine were in the same settings as the development machines, the code was executing perfectly.
When the code was running on a Japanese machine something different happened. While creating an instance of DerivedControl the BaseControl constructor was called. BaseControl constructor called its’ InitializeComponents method and as a result the OnResize method of DerivedControl was called. The OnResize method was called due to lines 14 and 15 of BaseControl since the screen resolution or font settings were different from the development machine settings. Since someString was not initialized yet, a NullReferenceException has been thrown.
To fix this problem we simply set the AutoScaleMode property to AutoScaleMode.None since we don’t need Automatic Scaling support. Of course it took us a while before we understood this problem and therefore I decided to share our experience.
- Beware from the AutoScaleMode property.
- Test your WinForms application on different screen resolutions, DPI settings and Font setting.