-2

How to auto-fit my vb.net form into different screen resolutions? I created the form from desktop and it's perfectly fine it displays the entire form but when I open the form in laptop the form does not fit to laptop screen. The form's window state is normal and size is 1081, 780.

Hi @Jimi here you go.

Public Class Form1
' For screen size changes. 
Dim cw As Integer ' Forms current Width.
Dim ch As Integer ' Forms current Height.
Dim iw As Integer = 1280 ' Forms initial width.
Dim ih As Integer = 760 ' Forms initial height.
' Retrieve the working rectangle from the Screen class using the        
PrimaryScreen and the WorkingArea properties.  
Dim workingRectangle As System.Drawing.Rectangle =     
Screen.PrimaryScreen.WorkingArea

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles 
MyBase.Load
' Set the size of the form slightly less than size of working rectangle. 
Me.Size = New System.Drawing.Size(workingRectangle.Width - 5, 
workingRectangle.Height - 5)
' Set the location so the entire form is visible. 
Me.Location = New System.Drawing.Point(3, 3)
End Sub

Private Sub Main_Resize(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles Me.Resize
' Change controls size and fonts to fit screen working area..
Dim rw As Double = (Me.Width - cw) / cw ' Ratio change of original form 
width.
Dim rh As Double = (Me.Height - ch) / ch ' Ratio change of original form 
height.
' Change controls size to fit users screen working area.
For Each Ctrl As Control In Controls
    Ctrl.Width += CInt(Ctrl.Width * rw)
    Ctrl.Height += CInt(Ctrl.Height * rh)
    Ctrl.Left += CInt(Ctrl.Left * rw)
    Ctrl.Top += CInt(Ctrl.Top * rh)
Next
cw = Me.Width
ch = Me.Height
' Change all the forms controls font size.
Dim nfsize As Single
If cw > iw + 500 Then
    For Each Ctrl As Control In Controls
        ' Get the forms controls font size's property and increase it. 
Reset the font to the new size. 
        nfsize = Me.Font.Size + 3
        Ctrl.Font = New Font(Ctrl.Font.Name, nfsize, FontStyle.Bold, 
Ctrl.Font.Unit)
    Next
Else
    Exit Sub
End If
End Sub
Thirdy Leon
  • 59
  • 1
  • 7
  • What does *doesn't fit* mean? Is it too large? Too small? Are you maximizing it? Setting the Size an Location *manually*? Did you design your app at 100% scale in the development machine? Is your app DpiAware? -- See the notes here: [Using SetWindowPos with multiple monitors](https://stackoverflow.com/a/53026765/7444103), it may help. – Jimi Jul 29 '21 at 23:32
  • 3secs to google this. https://learn.microsoft.com/en-us/dotnet/desktop/winforms/automatic-scaling-in-windows-forms?view=netframeworkdesktop-4.8 Do some research – Hursey Jul 29 '21 at 23:33
  • Hi @Jimi I created the vb.net form in desktop with 24 inches monitor and the size of the form occupies the screen about 60-70% (from the total size of the screen) but when I open the form from the 14 inches laptop screen it does not show the entire form. – Thirdy Leon Jul 30 '21 at 00:01
  • Did you design your app at 100% scale in the development machine? Yes. Is your app DpiAware? Yes – Thirdy Leon Jul 30 '21 at 00:20
  • `workingRectangle` cannot be a Field. Get that measure in Form.Load and again each time you need it, don't try to store it. It only works if your app is actually DpiAware when the Screen Dpi changes. Of course your Form needs to scale to Dpi. -- All that stuff in Resize is not needed if you design the Form layout correctly: use TableLayoutPanels, FlowLayoutPanels, group Control that don't need to scale in other containers (Panels. mainly). -- Make use of the `MinimumSize` and `MaximumSize` properties (often neglected). -- You should specify your .Net version and how DpiAwaresess is activated. – Jimi Jul 30 '21 at 01:16

2 Answers2

0

With the AutoSize property of the form. Please refer to the docs: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.autosize?view=net-5.0

Juanjo
  • 670
  • 7
  • 17
0

Just add this code in the event Form_Load :

Me.WindowState = FormWindowState.Maximized
zdlk
  • 21
  • 4