繁体   English   中英

表单不能动态显示

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM