简体   繁体   中英

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. 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

private void InitializeComponent()
{
   this.BuildComponents(questions);

            foreach (var property in propertyList)
            {
                this.Controls.Add(property);
            }
} 

I would recommend you to use FlowLayoutPanel and UserControls. With FlowLayoutPanel you can put usercontrols one after another and you dont need to deal with the location property.

In addition, you should not change InitializeComponent.. Actually, you dont need to touch the code in the designer file!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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