mardi 24 mai 2016

Which Design pattern for dynamical creating buttons

I have created a custom user control that adds combo boxes to itself.

My custom user control has 1 button which Adds Two Combo Boxes.

Clicking on it adds 2 combo boxes under the last two.

When the selected index is changed on any of the combo boxes it will run a calculation using the selected values in each box.

This is my code so far...

 public partial class PlateSectionAdder : UserControl
    {

        private List<Dictionary<string, CueComboBox>> listOfTwoCombos = new List<Dictionary<string, CueComboBox>>();

        public List<string> ColList { get; }
        public List<string> PlateList { get; }


        private int count = 1;
        private int gap = 3;
        private int edge = 5;

        private Button btnAdd = new Button();


        public PlateSectionAdder(List<string> collist, List<string> platelist)
        {
            InitializeComponent();
            this.ColList = collist;
            this.PlateList = platelist;

            btnAdd.FlatStyle = FlatStyle.Flat;
            btnAdd.Top = edge / 2;
                btnAdd.Left = edge;
                btnAdd.Width = this.Width - (edge * 2);
            btnAdd.Click += BtnAdd_Click;
                this.Controls.Add(btnAdd);
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            CueComboBox cmbCols = new CueComboBox();
                cmbCols.DataSource = this.ColList;
                cmbCols.Top = (btnAdd.Bottom + edge) * count;
                cmbCols.Left = edge;
                cmbCols.Width = btnAdd.Width / 2 - gap;
            cmbCols.SelectedIndexChanged += CalculatePlateSections;

            CueComboBox cmbPlates = new CueComboBox();
                cmbPlates.DataSource = this.PlateList;
                cmbPlates.Top = (btnAdd.Bottom + edge) * count;
                cmbPlates.Left = cmbCols.Right + gap;
                cmbPlates.Width = btnAdd.Width / 2 - gap;
                cmbCols.DataSource = ColList;


            this.Controls.Add(cmbCols);
            this.Controls.Add(cmbPlates);

            this.Height = btnAdd.Height + (cmbCols.Height * count) + edge + edge + 1;
                cmbCols.DataSource = ColList;
                cmbPlates.DataSource = PlateList;


            cmbPlates.SelectedIndexChanged += CalculatePlateSections;

            count++;
        }

        private void CalculatePlateSections(object sender, EventArgs e)
        {
            MessageBox.Show("Test");
        }

The combo boxes seem to be copies of the same instance, rather than independent, I thought by using the new keyword they would be each there own instance.

I am sure there a design pattern out there that would be right for this but not sure which one.

Has a look at the builder pattern but it does not seem exactly what I am looking for.

if there is anything unclear about this let me know and I will clarify.

Aucun commentaire:

Enregistrer un commentaire