Looking for example(s) of automatic scaling of controls within a Visual Basic 2022 form

Ron Sipherd 226 Reputation points
2023-05-03T03:17:32.52+00:00

With Visual Basic 2022 (.NET Framework 4.8, Windows Forms Application) I am modifying an older utility I wrote, and I would like it to take advantage of different screen sizes.

I want to scale the contents (labels, list boxes, text boxes, etc.) within a VB form. But not one by one.

If necessary I could treat the problem as involving just two display resolutions, one of which is 1.5 times larger in each dimension compared to the other:

Desktop form client size: 1920 x 1137

Laptop form client size: 1280 x 657

(I can approximate but not exceed these values.)

The help pages for

Automatic scaling in Windows Forms

Adjusting the size and scale of Windows Forms

do not include examples; one or two of those would be very useful. Do they exist, and where?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,403 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 23,961 Reputation points Microsoft Vendor
    2023-05-03T07:13:28.88+00:00

    @Ron Sipherd, Welcome to Microsoft Q&A, you could refer to the following link to set Automatic scaling in a windows form.

    Automatic scaling in action

    You could look at the following Microsoft Learning to use the desktop font.

    How to: Respond to Font Scheme Changes in a Windows Forms Application

    Update:

    If you want to achieve the function Automatic scaling in Windows Forms, I recommend that you use the following code in constructor:

    public Form1()
            {
                InitializeComponent();
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.Font = SystemFonts.DefaultFont;
                this.AutoScaleDimensions = new SizeF(96F, 96F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
            }
    

    If you want to adjust the size and scale of Windows Forms, I recommend that you used Control.Scale Method. Here is a code example you could refer to.

     [Obsolete]
            private void Form1_Load(object sender, EventArgs e)
            {
                TableLayoutPanel panel = new TableLayoutPanel();
                panel.Dock = DockStyle.Fill;
                panel.RowCount = 2;
                panel.ColumnCount = 2;
                panel.Controls.Add(new Label() { Text = "Name:" }, 0, 0);
                panel.Controls.Add(new TextBox(), 1, 0);
                panel.Controls.Add(new Label() { Text = "Age:" }, 0, 1);
                panel.Controls.Add(new TextBox(), 1, 1);
                panel.Scale(2f);
                this.Controls.Add(panel);
            }
    

    Example:

    User's image

    As the above result shown, all the controls are scaled by a factor of 2.

    Best Regards,

    Jack

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.  

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Ron Sipherd 226 Reputation points
    2023-05-06T20:49:59.8533333+00:00

    I think I have the keys to accomplish what I need to do. Have marked Mr. Jun's response as an answer.

    And I can expand a little on it, though a work in progress, not a finished product.

    Dim fontFactor As Single = 1.25
    
    Private Sub frmResizer_Shown(sender As Object, e As EventArgs) Handles Me.Shown
      me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    End Sub
    
    Private Sub cmdChange_Click(sender As Object, e As EventArgs) Handles cmdChange.Click
      me.Font = New Font(Me.Font.Name, Csng(Me.Font.Size * fontFactor), Me.Font.Style)
      ' Change font size of each control here
      Dim CtrlsCount As Integer = Me.Controls.count
      For Ix As integer = 0 To (CtrlsCount- 1)
        Me.Controls.Item(Ix).Font =  _
            New Font(Me.Controls.Item(Ix).Font.Name, CSng(Me.Controls.Item(Ix).Font.size * fontFactor), Me.Controls.Item(Ix).Font.style)
      Next
    End Sub
    
    
    
    0 comments No comments