1

我需要将枚举值生成为如下单选按钮,但对于 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 回答 1

2

可能你需要这条线

radioButtonY = 10;

退出内部循环,否则您将始终获得相同的 Y 值,并且您的按钮堆叠在一起

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

}
于 2013-05-22T12:59:40.770 回答