patterncsharpMinor
Dynamically adding controls to a form in a WinForms project
Viewed 0 times
dynamicallycontrolsaddingwinformsprojectform
Problem
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, or if coding it this way is the correct approach. I know that it works because I have tested it.
My application generates a questionnaire based on selected (cyber security) standards. I have a form
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, which is why I put it in here.
```
// Load answer input according to the number of questions standards selected.
private void questionStandardInput_MouseUp(object sender, MouseEventArgs e)
{
questionLabels.Clear();
maturityInput.Clear();
complianceInput.Clear();
selectionPanel.Controls.Clear();
foreach (var item in questionStandardInput.CheckedItems)
{
Label qs_label = new Label();
qs_label.Location = new Point(0, 0 + (questionLabels.Count * 115));
qs_label.Font = new Font("Arial", 12F, FontStyle.Regular);
qs_label.Size = new Size(150, 18);
qs_label.Text = item.ToString();
questionLabels.Add(qs_label);
this.selectionPanel.Controls.Add(qs_label);
Label m_label = new Label();
m_label.Location = new Point(0, 0 + (questionLabels.Count * 115 + 35));
m_label.Font = new Font("Arial", 12F, FontStyle.Regular);
m_label.Size = new Size(150, 18);
m_label.Text = "Maturity standa
My application generates a questionnaire 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, which is why I put it in here.
```
// Load answer input according to the number of questions standards selected.
private void questionStandardInput_MouseUp(object sender, MouseEventArgs e)
{
questionLabels.Clear();
maturityInput.Clear();
complianceInput.Clear();
selectionPanel.Controls.Clear();
foreach (var item in questionStandardInput.CheckedItems)
{
Label qs_label = new Label();
qs_label.Location = new Point(0, 0 + (questionLabels.Count * 115));
qs_label.Font = new Font("Arial", 12F, FontStyle.Regular);
qs_label.Size = new Size(150, 18);
qs_label.Text = item.ToString();
questionLabels.Add(qs_label);
this.selectionPanel.Controls.Add(qs_label);
Label m_label = new Label();
m_label.Location = new Point(0, 0 + (questionLabels.Count * 115 + 35));
m_label.Font = new Font("Arial", 12F, FontStyle.Regular);
m_label.Size = new Size(150, 18);
m_label.Text = "Maturity standa
Solution
In my eyes it is an appropriated way to build up your form that way. The
Just a few points:
-
There are some numbers (115, 70, 35, ...) that seems to have a special meening (e.g. row height, margin, whatever). IMHO it makes sense to store that numbers as constants with a meaningful name and use that constants instead.
-
I am not sure if the
-
There is some repeating logic for creating the
.Designer file is for designer generated code - but if you need to add custom logic to modify your GUI the .cs file is the correct location.Just a few points:
-
There are some numbers (115, 70, 35, ...) that seems to have a special meening (e.g. row height, margin, whatever). IMHO it makes sense to store that numbers as constants with a meaningful name and use that constants instead.
-
I am not sure if the
MouseUp event is the right place to trigger that kind of logic. If there is another event (CheckBoxChanged, SelectedItemChanged, ...) I would suggest to use that.-
There is some repeating logic for creating the
ComboBox / Label and so on. That logic could be extracted to a separted method. For instance:private static ComboBox CreateComboBox()
{
return new ComboBox()
{
Font = new Font("Arial", 12F, FontStyle.Regular)
Size = new Size(200, 22)
DropDownStyle = ComboBoxStyle.DropDownList
}
}Code Snippets
private static ComboBox CreateComboBox()
{
return new ComboBox()
{
Font = new Font("Arial", 12F, FontStyle.Regular)
Size = new Size(200, 22)
DropDownStyle = ComboBoxStyle.DropDownList
}
}Context
StackExchange Code Review Q#143393, answer score: 7
Revisions (0)
No revisions yet.