Question How to properly display dynamic controls

Marthe

New member
Joined
Oct 4, 2016
Messages
2
Programming Experience
1-3
Hi,

I have created this function in my DetailsScreen.cs, but i have no idea if this is the correct file to put this kind of code in, and if the code is even 'correct'. I know that it works because i have tested it. Please help me, i am only a student doing an internship and i can not get any proper help.

More details: My application generates a questionnare based on selected (cyber security) standards. I have a form DetailsScreen, which requires the user to fill in some details such as name, company, etc. They also need to select from a checkedlist (questionStandardInput), the question standard they wish to use in this interview. For each selected standard, two answer standards need to be selected from two drop down lists (maturity answer standard and compliance answers standard). Therefore, i need to be able to dynamically add labels and comboxes for each question standard. I asked because most code concerning label font etc. is placed in .Designer.cs files instead of the .cs files. But i need certain logic from the .cs file in order to properly format the controls, that's why i put it in here.

C#:
private void questionStandardInput_MouseUp(object sender, MouseEventArgs e)
        {
            questionLabels.Clear();
            maturityInput.Clear();
            complianceInput.Clear();
            selectionPanel.Controls.Clear();
            
            foreach (var item in questionStandardInput.CheckedItems)
            {
                Label m_label = new Label();
                m_label.Location = new Point(0, 0 + (questionLabels.Count * 115 + 35));
                m_label.Name = item.ToString() + "MLabel";
                m_label.Font = new Font("Arial", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
                m_label.Size = new System.Drawing.Size(150, 18);
                m_label.Text = "Maturity standard";
                this.selectionPanel.Controls.Add(m_label);


                ComboBox m_input = new ComboBox();
                m_input.Location = new Point(170, 0 + (questionLabels.Count * 115 + 35));
                m_input.Name = item.ToString() + "MLabel";
                m_input.Font = new Font("Arial", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
                m_input.Size = new System.Drawing.Size(200, 22);
                m_input.DropDownStyle = ComboBoxStyle.DropDownList;
                maturityInput.Add(m_input);
                this.selectionPanel.Controls.Add(m_input);


                Label c_label = new Label();
                c_label.Location = new Point(0, 0 + (questionLabels.Count * 115 + 70));
                c_label.Name = item.ToString() + "CLabel";
                c_label.Font = new Font("Arial", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
                c_label.Size = new System.Drawing.Size(170, 18);
                c_label.Text = "Compliance standard";
                this.selectionPanel.Controls.Add(c_label);


                ComboBox c_input = new ComboBox();
                c_input.Location = new Point(170, 0 + (questionLabels.Count * 115 + 70));
                c_input.Name = item.ToString() + "MLabel";
                c_input.Font = new Font("Arial", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
                c_input.Size = new System.Drawing.Size(200, 22);
                c_input.DropDownStyle = ComboBoxStyle.DropDownList;
                complianceInput.Add(c_input);
                this.selectionPanel.Controls.Add(c_input);
                
                Label qs_label = new Label();
                qs_label.Location = new Point(0, 0 + (questionLabels.Count * 115));
                qs_label.Name = item.ToString() + "QSLabel";
                qs_label.Font = new Font("Arial", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
                qs_label.Size = new System.Drawing.Size(150, 18);
                qs_label.Text = item.ToString();
                questionLabels.Add(qs_label);
                this.selectionPanel.Controls.Add(qs_label);


                InitializeMaturityStandardInput(m_input);
                InitializeComplianceStandardInput(c_input);
            }
            this.saveAssessmentButton.Location = new Point(235, 0 + (questionLabels.Count * 115));
            this.selectionPanel.Controls.Add(saveAssessmentButton); 
        }

Greetings,

Marthe
 
Last edited:
It might be easier for us to know whether the code is correct if we knew what it was supposed to. Maybe you should provide a more detailed explanation rather than have us work it out from code that may or may not be correct. Also, we can't really tell you whether DetailsScreen.cs is the right place to put the code as, again, we don't know what DetailsScreen.cs actually is. Presumably it's the code file for a form but, beyond that, we can only guess. Given that you already know, it would be better if you explained it rather than our guessing.
 
I would suggest that you create user controls for each set of controls that you know you may need. A user control is like a form in that you create it in the designer, adding controls and code, but it is just like any other control in that you can add it to a form or another user control from the Toolbox.
 
Back
Top Bottom