只有4个Enum值的1个按钮,为什么?

时间:2013-05-22 12:54:00

标签: c# .net user-interface

我需要将Enum值生成为单选按钮,如下所示,但对于一个4,5值的prop,我只得到一个带有1个按钮的groupbox,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);
}

1 个答案:

答案 0 :(得分:2)

可能你需要这一行

radioButtonY = 10;

离开内循环,否则你总是得到相同的Y值,你的按钮堆叠在一起

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

}