Problem in the size of the form and controls at the time of execution

Mohammad Hasan Salmanian 45 Reputation points
2023-09-11T10:17:47.9166667+00:00

I have created a form in the form of image 1, but at the time of execution, the distance between the controls increases and the size of the form also increases. What is the problem? Image 2 is at runtime.

I have also used TableLayoutPanel to place the controls in the form and the sizes are according to picture 3.

What should I do so that the size of the form and the distance of the controls and the size of the controls are the same as the Design environment?

Image1:

enter image description here

Image2:

enter image description here

Image3:

enter image description here

public BaseForm()
{
   InitializeComponent();
            
       Font = new Font("IRANSans", 15 , GraphicsUnit.Pixel);
       AutoScaleDimensions = new SizeF(5, 5);
       AutoScaleMode = AutoScaleMode.Font;
       FormBorderStyle = FormBorderStyle.FixedDialog;
       MaximizeBox = false;
       StartPosition = FormStartPosition.CenterScreen;
       AutoSizeMode = AutoSizeMode.GrowAndShrink;
       Width = 477;
       Height = 418;
       InitializeComponent();
}
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,857 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 44,751 Reputation points Microsoft Vendor
    2023-09-11T14:05:10.5366667+00:00

    Hi @Mohammad Hasan Salmanian , Welcome to Microsoft Q&A,

    The first three lines of your code are the key to your form's changes.

    Font = new Font("IRANSans", 15, GraphicsUnit.Pixel);This line of code creates a font named "IRANSans" with a size of 15 pixels and assigns it to the Font of the form or control Attributes. This means that when the text content of a form or control is displayed, the "IRANSans" font will be used, and the size of the text will be 15 pixels.

    AutoScaleDimensions = new SizeF(5, 5); This line of code sets the auto-scaling dimensions. Typically, automatic scaling is used to ensure that forms and controls maintain a consistent appearance across different display resolutions. AutoScaleDimensions is used to specify to what extent scaling is performed, here setting to (5, 5) means that 5 logical units (usually device-independent pixels) in both horizontal and vertical directions are equal to one physical unit (usually inches ).

    AutoScaleMode = AutoScaleMode.Font; This line of code sets the autoscaling mode. The AutoScaleModeproperty specifies when and how to automatically scale a form or control. Here, it is set to AutoScaleMode.Font, which means that the autoscaling will adjust based on the size of the font to ensure a consistent appearance across different font sizes.

    If you delete the first line of font settings, you will find that the form does not change.

    You'd better design in the target style so it won't change at runtime.

    DPI may also be a direction for your research.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    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.

    0 comments No comments

  2. KOZ6.0 6,395 Reputation points
    2023-09-11T21:46:57.82+00:00

    Is BaseForm inherited from Telerik.WinControls.UI.RadForm?

    RadForm changed font. Changing it in BaseForm will cause trouble.

    You can paste Panel(Dock = DockStyle.Fill) and change the font of Panel.

    Design is done on Panel.

    And DefaultSize Property is overriddable

            protected override Size DefaultSize => new Size(477, 418);       
            ```
    
    
    0 comments No comments

Your answer

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