首页 > 解决方案 > 添加到 Form.Controls 时,RadioButton 会更改其高度

问题描述

我在一个循环中以编程方式创建了许多 RadioButtons。我设置了它的一些属性,最后将新创建的 RadioButton 添加到 Form.Controls 列表中。在我将 RadioButton 添加到 Form.Controls 之前,它的 Height 属性是 24。在我将 RadioButton 添加到 Form.Controls 之后的一行,它的 Height 属性是 29。这是循环:

for (int i = 0; i < numberOfRadioButtons; i++)
{
    RadioButton currentRadioButton = new RadioButton();
    currentRadioButton.Font = new Font("Times New Roman", 16, FontStyle.Bold);
    currentRadioButton.Name = "radioButton" + (i + 1).ToString();
    currentRadioButton.Text = currentRadioButton.Name;
    currentRadioButton.AutoSize = true;
    int currentRadioButtonHeight = currentRadioButton.Height; // 24
    this.Controls.Add(currentRadioButton);
    currentRadioButtonHeight = currentRadioButton.Height; // 29
}

我不明白向 Form.Controls 添加控件如何更改控件高度的值。请帮忙。先感谢您。

标签: c#winformsradio-button

解决方案


我发现!该声明:

currentRadioButton.AutoSize = true;

改变高度。如果声明是:

currentRadioButton.AutoSize = false;

高度不变。


推荐阅读