c# - 为动态创建的控件设置 anchor

标签 c# winforms radio-button

我有 winform,上面动态创建了 52 个单选按钮。 这是创建它们的方法:

    private void CreateRadioButton()
    {            
        int rbCount = 52;
        int numberOfColumns = 23;

        radioButtons = new RadioButton[rbCount];
        int y = 520;

        for (int i = 0; i < rbCount; i++)
        {
            radioButtons[i] = new RadioButton();
            radioButtons[i].Text = Convert.ToString(i + 1);

            if (i % numberOfColumns == 0) 
                y += 20;

            var x = 11 + i % numberOfColumns * 50;

            radioButtons[i].Location = new Point(x, y);
            radioButtons[i].Size = new Size(40, 15);
            //radioButtons[i].Anchor = AnchorStyles.Left;
            //radioButtons[i].Anchor = AnchorStyles.Bottom;
            radioButtons[i].Font = new Font(radioButtons[i].Font.FontFamily, 8, FontStyle.Bold);
            radioButtons[i].UseVisualStyleBackColor = true;
            radioButtons[i].Click += new EventHandler(rbtns_click);

            xtraTab.Controls.Add(radioButtons[i]);            
        }          

    }

窗体最大化时出现问题。单选按钮消失。 如果我设置

 radioButtons[i].Anchor = AnchorStyles.Left;
 radioButtons[i].Anchor = AnchorStyles.Bottom;

单选按钮被覆盖。

如果调整表单大小,我该怎么做才能将它们的位置保持在同一位置?

最佳答案

这两行

radioButtons[i].Anchor = AnchorStyles.Left;
radioButtons[i].Anchor = AnchorStyles.Bottom;

意味着 anchor 值 AnchorStyles.Left 将被替换为 AnchorStyles.Bottom

AnchorStylesFlags 属性集,枚举值可以组合:

radioButtons[i].Anchor = AnchorStyles.Left | AnchorStyles.Bottom;

如果通过设计器设置,在“Windows 窗体设计器生成的代码”中它看起来像这样:

this.radioButton1.Anchor = ((System.Windows.Forms.AnchorStyles)
   ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));

关于c# - 为动态创建的控件设置 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42827753/

相关文章:

c# - 如何禁用 ASP.NET 中的下拉列表?

c# - GetFunctionPointerForDelegate 将委托(delegate)中的 String^ 参数转换成什么?

c# - 我如何在 dot net Core 中排列应用程序 dll 文件

c# - 在实时图表中设置轴的最小值

html - 如何通过 "for"属性更新 Prototype 中 <label> 的 innerHTML?

jquery - 如何使用 Jquery 获取多个选定的单选按钮值

css - 如何设置禁用的单选按钮的样式?

c# - 使用不存在的模型实体返回结果 - EF .net Core 3

c# - 部分类中的自定义属性(无效的列名称)

c# - WinForm 图表控件 : Change size of chart when saving it to a file