简体   繁体   English

表单不能动态显示

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

相关问题 Windows 窗体无法显示 CenterScreen - Windows Form Cannot be Displayed CenterScreen 已经可见的窗体不能显示为模式对话框 - form that is already visible cannot be displayed as a modal dialog box 动态添加控件不会以asp.net Web形式显示在面板内部 - dynamically adding controls does not displayed inside panel in asp.net web form 不是顶级窗体的窗体不能显示为模式对话框。 在调用showDialog之前,从任何父表单中删除该表单 - Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog 动态将复选框添加到表单时,无法调整复选框的大小 - Cannot resize checkbox when dynamically adding checkboxes to form SVG 无法显示为图像 - SVG cannot be displayed as an image 在Silverlight中无法显示图像 - Image Cannot be Displayed in Silverlight 什么时候显示表格? - When is a form displayed? SharePoint 2010-此页面上的Web部件或Web窗体控件无法显示或导入。 该类型未注册为安全 - SharePoint 2010 - A Web Part or Web Form Control on this Page cannot be displayed or imported. The type is not registered as safe Web 部件错误:无法显示或导入此页面上的 Web 部件或 Web 窗体控件 - Web Part Error: A Web Part or Web Form Control on this Page cannot be displayed or imported
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM