VB.Net adjusting forms and controls to display resolution

Claude Larocque 666 Reputation points
2021-10-23T11:39:17.653+00:00

There are many explanations on how to develop an application that will use the display resolution to adjust automatically its forms and controls.

But it becomes confusing and I tried so many things that didn't work so my question is:

Someone knows an example (solution) that works perfectly with the display resolution an .sln file or .csproj
in VB.Net?

If I would have an example of one form, then I can apply the configuration to all my forms.

Claude from Quebec, Canada
Thanks in advance
******@autocaisse.onmicrosoft.com

Developer technologies | VB
{count} votes

Answer accepted by question author
  1. Karen Payne MVP 35,596 Reputation points Volunteer Moderator
    2021-10-24T19:22:07.523+00:00

    My two cents is that (and I realize this is not what you are working with) WPF handles resizing a good deal better than Windows forms, kind of like responsive design for a web app say using Bootstrap with containers.

    What I've seen work pretty good is a third party library from a company known as Progress and in the following blog there is a good discussion steps to follow for auto resizing with and without their product.

    WinForms Scaling at Large DPI Settings–Is It Even Possible?

    Unfortunately most developer are squeamish about purchasing products to make their life easier and that is okay yet they miss out on simple solutions.

    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. 40273900 1 Reputation point
    2022-02-06T14:22:27.373+00:00

    I ran into the same problem with the form not fitting on the screen.
    I solved my problem like this:
    Dim screenwidth As Integer = My.Computer.Screen.Bounds.Width
    'screenwidth on my computer is 1600. The controls have been designed for that width.
    factor = screenwidth / 1600 'this is screenwidth of computer where the program has been installed.
    If factor < 1 Then
    For Each control In Controls
    control.width = factor control.width
    control.left = factor control.left
    Next
    End If

    It is amazingly simple so maybe this is not what you are looking for.


  2. Stress Engineer 0 Reputation points
    2023-01-21T16:15:38.2833333+00:00

    Somewhere in all my clicking about I found this solution to a slightly different problem, but I think it can be easily adapted. To resize all the elements of a form when the form is resized do this:

    'At the beginning of the form:
    
        Private isLoading As Boolean = True
        Private origFormSize As Rectangle
        Private originalControlRectangles(100) As Rectangle
        Private originalControlFontSizes(100) As Single
        Private FontScale As Single = 1.0
    
    'In the form.load event:
    
            origFormSize = New Rectangle(Me.Location, Me.Size)
            Dim controlCount As Integer
            Dim ctrl As Control = Me.GetNextControl(Me, True)
            Do Until ctrl Is Nothing
                originalControlRectangles(controlCount) = New Rectangle(ctrl.Location, ctrl.Size)
                originalControlFontSizes(controlCount) = ctrl.Font.Size
                controlCount += 1
                ctrl = Me.GetNextControl(ctrl, True)
            Loop
            isLoading = False
    
    'In the form.resize event:
    
            If isLoading Then Exit Sub
            Dim controlCount As Integer
            Dim ctrl As Control = Me.GetNextControl(Me, True)
            Do Until ctrl Is Nothing
                ResizeChildControl(ctrl, originalControlRectangles(controlCount), originalControlFontSizes(controlCount))
                controlCount += 1
                ctrl = Me.GetNextControl(ctrl, True)
            Loop
    
    'Add the method:
    
     Private Sub ResizeChildControl(ByRef aControl As Control, ByRef origControl As Rectangle, ByRef origFontSize As Single)
            Dim xRatio As Single, yRatio As Single
            Try
                xRatio = Me.ClientRectangle.Width / origFormSize.Width
                yRatio = Me.ClientRectangle.Height / origFormSize.Height
    
                Dim newX As Single = origControl.Location.X * xRatio
                Dim newy As Single = origControl.Location.Y * yRatio
    
                aControl.Location = New Point(CInt(newX), CInt(newy))
                aControl.Width = CInt(xRatio * origControl.Width)
                aControl.Height = CInt(yRatio * origControl.Height)
    
                Dim ratio As Single
                If xRatio >= yRatio Then
                    ratio = yRatio
                Else
                    ratio = xRatio
                End If
    
                Dim newFontSize As Single
                newFontSize = origFontSize * ratio * FontScale
                Dim newFont As Font
                newFont = New Font(aControl.Font.FontFamily, newFontSize)
                aControl.Font = newFont
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
        End Sub
    

    I'll say again: I did not write this code, but I have tested it. It works well for smaller Windows forms, but not forms with more than, say, 25 controls.

    Thank you to whoever did write this code it has saved me hours of form design time.

    0 comments No comments

  3. LesHay 7,146 Reputation points
    2023-02-28T16:16:34.7033333+00:00

    Hi

    I have had similar issues as OP and over a long time I have periodically tried various options from articles on the internet. Many did not offer any real improvement and a few offered some improvement. I gradually collected from several articles, some code that I put together to try and get a solution.

    I have posted a link here to the end result. This has been the best that I have managed to find.

    I only have one computer (well, 2 actually but both same screen), so my testing has been limited to trying the code out on various resolutions, screen scales and text scales and this code copes well with all of those changes. I had to fins a way to be able to compare from one setting so another, so I incorporated the ideas into an 'On Screen Ruler' application - it does provide the necessary means of comparison.

    For anyone that want to try it out, the entire project is Zipped as a Public OneDrive Share at

    https://1drv.ms/f/s!AgEpHyV3DNo7gphOEZyxIvRHz4E2Gg


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.