1
我需要生成如下的單選按鈕的枚舉值,但對於一個4,5值的道具只能得到1個按鈕item1的groupbox。我錯在哪裏?只能從4個Enum值中獲得1個按鈕,爲什麼?
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);
}
不錯,謝謝。任何關於如何使這個顯示整齊的建議?也許水平的按鈕,新的組合框總是在他們的正下方?我正在嘗試但沒有完全到達那裏,它們看起來很混亂 – justanidiot
很難說不知道枚舉文本以及這個代碼預計會創建多少個組合框和單選按鈕。 – Steve
以及我也使用gb.Width,高度和按鈕。但不知何故,我似乎把他們放在錯誤的地方,因爲輸出是隨機的。此外,由於這是在運行時,我在頂部使用了Controls.Clear,所以每次有新的對象被饋入時,它都會重新填充數據。但似乎是錯誤的因爲現在當我在運行時移動到下一個對象時,所有東西都消失了 – justanidiot