private void DoStuffWhenSelectingAField(object sender, ItemCheckEventArgs e) { if (!(listbox.Items[e.Index] is Attribute selectedAttribute)) { return; } if (e.CurrentValue == CheckState.Checked) { _selectedAttributes.Remove(selectedAttribute); for (var i = panel2.Controls.Count - 1; i >= 0; i--) { if (panel2.Controls[i].Name.Contains(selectedAttribute.LogicalName)) { panel2.Controls.RemoveAt(i); } } if (panel2.Controls.Count == 1) { panel3.Visible = false; lblDataType.Visible = false; } return; } lblDataType.Visible = true; _selectedAttributes.Add(selectedAttribute); List <string> validTypes; try { validTypes = DataTypeHelper.GetValidTypes(selectedAttribute.TypeCode); } catch (NotImplementedException exception) { e.NewValue = e.CurrentValue; MessageBox.Show(exception.Message); return; } var l = new Label { Font = _defaultFont, Margin = new Padding(10, 3, 3, 0), Text = selectedAttribute.DisplayName, Dock = DockStyle.Top, Name = $"lbl{selectedAttribute.LogicalName}", Anchor = AnchorStyles.Top, Width = panel2.Width }; var c = new ComboBox { Dock = DockStyle.Top, Margin = new Padding(10, 3, 3, 20), Font = _defaultFont, Name = $"{selectedAttribute.LogicalName}", Anchor = AnchorStyles.Top, Width = Convert.ToInt32(panel2.Width * 0.8), DropDownStyle = ComboBoxStyle.DropDownList }; c.SelectedValueChanged += (o, args) => { selectedAttribute.SetTypeOfData(c.Text); }; foreach (var validType in validTypes) { c.Items.Add(validType); } c.SelectedItem = selectedAttribute.GetDefaultDataType(); panel2.Controls.Add(l); panel2.Controls.Add(c); panel3.Visible = true; }