visual studio – C#/WinForms Dialog Units, Dynamic control location


Windows Forms support auto-scaling so if the user increases “font size” in Windows preferences, the form will automatically rescale. Now, when you create a window, add a button and this button has some predefined location, eg. x=100 y=200 this location will be represented in pixels on the system where the window was initially created. We can call it “Dialog Units”. On the initial project 1 dialog unit = 1 pixel. No longer holds when you change “font size”.

You can create controls dynamically and specify location to be eg. x=100 y=300 to put something else below that button. This will work only as long as the font size is the same as on the system of the user who created the form.

Now when you change your font size settings, the layout will break as controls added via Forms.Controls.Add(...) are not rescaled. How to force rescaling for dynamically added controls, so they’re not landing on “random” position which depends on windows font scaling preference?

Is there some method to convert Dialog Units to pixels?

Leave a Reply

Your email address will not be published. Required fields are marked *