[英]Form cannot be displayed dynamically
我試圖構建一個方法來動態繪制我的表單,這個方法接收一個問題列表,我們從中繪制一個表單並顯示每個問題(標簽)和一個選項(是/否 - 單選按鈕)。 我可以在我的 Forms.Controls 中添加之前創建的每個控件,但是當表單打開時,只呈現一個問題並傳遞一個包含 20 多個問題的列表。 為什么? 我是不是忘記做某事了?
此方法根據我的問題列表將我的所有組件構建到表單中。
private void BuildComponents(List<Question> properties)
{
this.propertyList = new List<System.Windows.Forms.Control>();
for (int i = 0; i < properties.Count; i++)
{
var newLabel = new System.Windows.Forms.Label
{
AutoSize = true,
Location = new System.Drawing.Point(13 + i + 5, 13),
Name = properties[i].Label,
Size = new System.Drawing.Size(699, properties[i].Description.Length),
TabIndex = i,
Text = properties[i].Description,
};
var newYesRadioButton = new System.Windows.Forms.RadioButton
{
AutoSize = true,
Location = new System.Drawing.Point(13 + i + 5, 34),
Name = "radioButton" + i + 1,
Size = new System.Drawing.Size(52, 21),
TabIndex = i + 1,
TabStop = true,
Text = "Sim",
UseVisualStyleBackColor = true
};
var newNoRadioButton = new System.Windows.Forms.RadioButton
{
AutoSize = true,
Location = new System.Drawing.Point(71 + i + 5, 34),
Name = "radioButton" + i + 2,
Size = new System.Drawing.Size(55, 21),
TabIndex = i + 1,
TabStop = true,
Text = "Não",
UseVisualStyleBackColor = true
};
propertyList.Add(newLabel);
propertyList.Add(newYesRadioButton);
propertyList.Add(newNoRadioButton);
};
}
此方法初始化我的表單並添加在 this.Controls 中構建的所有屬性
private void InitializeComponent()
{
this.BuildComponents(questions);
foreach (var property in propertyList)
{
this.Controls.Add(property);
}
}
我建議您使用 FlowLayoutPanel 和 UserControls。 使用 FlowLayoutPanel,您可以一個接一個地放置用戶控件,而無需處理位置屬性。
此外,您不應該更改InitializeComponent..實際上,您不需要觸摸設計器文件中的代碼!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.