[英]Form cannot be displayed dynamically
I trying to build a method to draw my form dynamically, this method receives a list of questions, from this we draw a form and show each question (label) and an option (yes/no - radio buttons).我试图构建一个方法来动态绘制我的表单,这个方法接收一个问题列表,我们从中绘制一个表单并显示每个问题(标签)和一个选项(是/否 - 单选按钮)。 I can add each control created before in my Forms.Controls, but when the form opens, just one question is rendered passing a list with more than 20 questions.
我可以在我的 Forms.Controls 中添加之前创建的每个控件,但是当表单打开时,只呈现一个问题并传递一个包含 20 多个问题的列表。 Why?
为什么? Did I forget to do something?
我是不是忘记做某事了?
This method builds all my components to the form based on my list of questions.此方法根据我的问题列表将我的所有组件构建到表单中。
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 method initializes my form and add all properties built in this.Controls此方法初始化我的表单并添加在 this.Controls 中构建的所有属性
private void InitializeComponent()
{
this.BuildComponents(questions);
foreach (var property in propertyList)
{
this.Controls.Add(property);
}
}
I would recommend you to use FlowLayoutPanel and UserControls.我建议您使用 FlowLayoutPanel 和 UserControls。 With FlowLayoutPanel you can put usercontrols one after another and you dont need to deal with the location property.
使用 FlowLayoutPanel,您可以一个接一个地放置用户控件,而无需处理位置属性。
In addition, you should not change InitializeComponent.. Actually, you dont need to touch the code in the designer file!此外,您不应该更改InitializeComponent..实际上,您不需要触摸设计器文件中的代码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.