AutoScaleMode.Font and Dynamically added controls

百般思念 提交于 2019-12-11 03:33:47

问题


I've been having some trouble scaling controls in my application properly with the form font size. The problem is that the form dynamically adds controls in response to user actions. Any controls that are on the form when the font size is initially set scale perfectly, but those added afterwards have issues. Their font scales properly, but their position and size don't. To see this in action, create a simple project with an empty form and paste in the following code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        SplitContainer split = new SplitContainer();
        split.Dock = DockStyle.Fill;
        this.Controls.Add(split);

        // Group 1
        split.Panel1.Controls.Add(NewGroup());

        this.Font = new Font(this.Font.FontFamily, this.Font.Size * 2);

        // Group 2
        split.Panel2.Controls.Add(NewGroup());

        split.SplitterDistance = this.Width / 2;
    }

    public GroupBox NewGroup()
    {
        GroupBox groupBox = new GroupBox();
        groupBox.Size = new System.Drawing.Size(132, 92);
        groupBox.Text = "groupBox";
        groupBox.SuspendLayout();

        Label label = new Label();
        label.AutoSize = true;
        label.Location = new Point(6, 16);
        label.Text = "label";
        groupBox.Controls.Add(label);

        Button button = new Button();
        button.Location = new Point(6, 58);
        button.Size = new Size(93, 28);
        button.Text = "button";
        groupBox.Controls.Add(button);

        CheckBox checkBox = new CheckBox();
        checkBox.AutoSize = true;
        checkBox.Location = new Point(47, 16);
        checkBox.Text = "checkBox";
        groupBox.Controls.Add(checkBox);

        TextBox textBox = new TextBox();
        textBox.Location = new Point(6, 34);
        textBox.Size = new Size(120, 20);
        textBox.Text = "text";
        groupBox.Controls.Add(textBox);

        groupBox.ResumeLayout();

        return groupBox;
    }
}

You can see the effect that I'm talking about in the second groupbox added. What can I do to get controls added after the initial size change to scale correctly?

UPDATE

If I change the second NewGroup call to look like this:

        GroupBox group = NewGroup();
        split.Panel2.Controls.Add(group);
        group.Scale(new SizeF(2.0f, 2.0f));

The result is ALMOST correct. It tends to be off by a pixel or two in a lot of cases, and in complex forms this starts to show up much more visibly. I really need the scaling to be as consistent as possible between controls, so I'd like to avoid this approach.


回答1:


The problem is, that the call to AutoScaleMode=AutoScaleMode.Font has to come AFTER all controls have been placed on the form. All controls you place after setting the AutoScaleMode are ignored from autoscaling. Generally, the Designer places the setting of the autoscalemode into the InitializeComponents()-Method, so every controls you create after the InitializeComponents()-Method fall into the ignored-category. Just remove the line from the InitializeComponents()-Method and replace it at the end of your forms constructor.

(even the question is old, the answer may help others)




回答2:


I think that you should do this - use Graphics.DpiX and Graphics.DpiY to get current dpi, then divide it with your default dpi (usually it is 96) and multiply position and size values for your dynamically added controls that don't appear right with this ratio.



来源:https://stackoverflow.com/questions/2298958/autoscalemode-font-and-dynamically-added-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!