从4个枚举值中只获得1个按钮,为什么?

本文关键字:按钮 1个 为什么 枚举 4个 | 更新日期: 2023-09-27 18:14:54

我需要生成枚举值作为单选按钮如下,但对于一个道具的4,5值我只得到一个组框与1个按钮,item1。我哪里错了?

private void CreateButton(List<object> prop) 
{
    GroupBox gb = new GroupBox();
    gb.Location = new Point(locationX, nextLocationY);
    gb.Name = "groupBox" + countControls;
    gb.Text = "some object";
    countControls++;
    foreach (var p in pr) 
    {
        ObjectType pType = p.Type;
        if (pType is Enum) 
        { 
            var TypesArray = Enumerable.ToArray(((Enum)pType).Values);
            foreach (var enumType in TypesArray) 
            {
                radioButtonY = 10;
                RadioButton rb = new RadioButton();
                rb.Appearance = Appearance.Button;
                rb.Width = rbWidth;
                rb.Height = rbHeight;
                rb.Name = enumType.Name + countControls;
                rb.Text = enumType.Name;
                countControls++;
                rb.Location = new Point(radioButtonX, radioButtonY);
                radioButtonY += rbHeight;
                gb.Controls.Add(rb);
                rb.CheckedChanged += rb_CheckedChanged;
            }
        }
    }
    gb.Height = 5 * rbHeight + 20;
    gb.Width = rbWidth + 20;
    nextLocationY += gb.Height + MARGIN;
    Controls.Add(gb);
}

从4个枚举值中只获得1个按钮,为什么?

可能你需要这一行

radioButtonY = 10;

不在内循环中,否则你总是得到相同的Y值并且你的按钮堆叠在一起

radioButtonY = 10;
foreach (var enumType in TypesArray) {
   .....
   radioButtonY += rbHeight;
}