Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Resolution Of Form Based On Screen Resolution ( without changing monitor resolution and using Maximized screen option )

I searched on the forums, and I tried a few things... but they didn't really seem to work. Let me lay out my problem.

I have a very high screen resolution on my laptop: 1400x1050. I'm designing my app on this.

My colleague tried it out on his laptop (which had lesser resolution), and the application did not fit on his laptop. The buttons were dragging out of the screen space.

So, I want my application to automatically resize/adjust based upon the screen resolution. I found some similar forums, and I tried a few things suggested by developers, but that did not really work out for me.

I tried : Solution 1 : But is changing user's screen resution in place of adjusting form .

I don't want to use Maximized screen option and don't want to change user's pc settings. Unfortunatly I am not using Table Layout panel.

Please suggest me a simple solution.

like image 501
Preeti Avatar asked Feb 26 '23 10:02

Preeti


2 Answers

OK, this is just about as simple as it gets. Just iterate through the VB controls and adjust their sizes based on the ratio of the new screen resolution to your design screen resolution. i.e., something like:

    Dim DesignScreenWidth As Integer = 1600
    Dim DesignScreenHeight As Integer = 1200
    Dim CurrentScreenWidth As Integer = Screen.PrimaryScreen.Bounds.Width
    Dim CurrentScreenHeight As Integer = Screen.PrimaryScreen.Bounds.Height
    Dim RatioX as Double = CurrentScreenWidth / DesignScreenWidth
    Dim RatioY as Double = CurrentScreenHeight / DesignScreenHeight
    For Each iControl In Me.Controls
        With iControl
            If (.GetType.GetProperty("Width").CanRead) Then .Width = CInt(.Width * RatioX)
            If (.GetType.GetProperty("Height").CanRead) Then .Height = CInt(.Height * RatioY)
            If (.GetType.GetProperty("Top").CanRead) Then .Top = CInt(.Top * RatioX)
            If (.GetType.GetProperty("Left").CanRead) Then .Left = CInt(.Left * RatioY)
        End With
    Next

NOTE that I'm using reflection to see if each control has the properties we need to adjust. The way I'm doing it is clean but uses "late binding" and requires Option Strict Off. Tested and approved.

like image 127
JoshuaDavid Avatar answered Feb 28 '23 00:02

JoshuaDavid


I know it's stupid but... did you try to set control "anchors"?

They allows your control to resize when you resize your form, maybe can help you, also think about using scrollbars

like image 41
Francesco Belladonna Avatar answered Feb 27 '23 23:02

Francesco Belladonna