Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Resize of Form based on Font size in VB.NET

Posted on 2005-03-03
Medium Priority
Last Modified: 2013-12-03
What is the best way to set the form size dynamically at initialization time based on the DPI to text size? Any ideas?

I have a form that is NOT re-sizable. It is a fixed form. But I need it to size dynamically based on the DPI settings.

In other words, if I was to change the desktop to 1280 x 1024 and the DPI settings to 120, my form should resize accordingly based on that information.

Any solutions?
Question by:cjinsocal581

Assisted Solution

lordicarus earned 1000 total points
ID: 13452424
in your load event you will want something along these lines...

        Me.Width = Screen.PrimaryScreen.WorkingArea.Width * 0.7
        Me.Height = Screen.PrimaryScreen.WorkingArea.Height * 0.5
        Dim g As Graphics = Me.CreateGraphics
        For Each mCtrl As Control In Me.Controls
            Dim mFnt As Font = mCtrl.Font
            mCtrl.Font = New Font(mFnt.FontFamily, sizeFormula)

I'm still trying to find out what "sizeFormula" would be... I'm not sure exactly how to convert a pixel font size to an em font size, but thats the basic idea.

Accepted Solution

Joe_Griffith earned 1000 total points
ID: 13453927
If you have the form's AutoScale property set to true then the form should automatically change size.

This doesn't work if you are explicitly setting the size in your code.

Author Comment

ID: 13453951
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 13454090
On my computer even with that on, it doesnt scale the form to display properly in different resolutions... it scales along with the resolution and gets bigger when i make my resolution smaller... what i mean by that is bringing my resolution from 1280x1024 to 800x600, the size of my test form remains 300 by 300, when the way I thought this question was to be interpreted was that in 1280x1024 the size should be 300 by 300 but in 800 by 600 it should be 187.5 by 175.78125(the same screen to form size ratio), and the fonts should also appropriately scale.  With Autoscale turned both on or off, this is not the behavior of my form.  Is this what you are saying happens Joe or am I misinterpreting you.

Expert Comment

ID: 13454122
A little further investigation seems that this only changes if you goto the Display settings in windows, goto the appearance tab, and change the font size from normal to extra large... at least this is what the documentation tells me... because this doesnt happen either.

Expert Comment

ID: 13454164
The screen resolution is not the issue.  If you change the resolution the form will appear bigger or smaller but it will still have the same size in pixels.

What causes the problem is changing the DPI setting (Display Properties, Advanced, General).

Most systems are set at 96DPI.  AutoScale will accomodate most other settings (120 is the one some people use) by changing the size of the form (although the change isn't perfect).  What doesn't work is if you explicitly set the size of the form in pixels in your code.  The autoscale feature then doesn't work and, typically going from 96 to 120 will result in part of the form being chopped off.

Expert Comment

ID: 13454830
I see what you are saying, but part of the changing the form size explicitly in code, you can use

Dim g As Graphics = Me.CreateGraphics

where g.DpiX is the dpi setting of the monitor.  With this value you can create that formula to automatically handle the DPI, but at the same time, you have to take into account the fact that that DPI isnt truly dots per inch.  True dots per inch is calculating how many pixels are on your screen and dividing it by the visible size of the screen in inches.  Using the AutoScale property only handles the "virtual" dpi.  You have to take into account true dpi as well.  Based on the way the question was asked I thought the solution needed was for the true dpi.  I agree with you completely, I think I just lost something in my interpretation of the question.

Expert Comment

ID: 21438352
can anybody have a sample code to locate form based on the true dpi?
please provides it urgently if possible.


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question