Dynamically populate and retrieve ComboBox items from Dictionary

User friendly client applications can be achieved by passing ComboBoxes items to other elements via the Dictionary object –
ComboBoxes are simply ‘Drop-down’ boxes so all the user needs to do is select an item with a familiar drop-down menu mechanism.

Scenario:

  • user is asked to put a predefined list of 6 items in their order of preference and then submit the list
  • user is then asked to create their own  list of 6 items and submit these in order of preference
Solution:

  • a UI with 6 'drop-down' menus each holding the 6 items
  • user clicks the first drop-down to select their first item preference,
  • clicks second drop-down for their second preference and so on.


Lab Example

1. Create a new Windows form application. In Form1.Designer.cs add code to build a combobox/button UI (hint: use the ‘copy to clipboard’ tool)

        #region Windows Form Designer generated code

        ///<summary> /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.combo_GundamSeason1 = new System.Windows.Forms.ComboBox();
            this.combo_GundamSeason2 = new System.Windows.Forms.ComboBox();
            this.combo_GundamSeason3 = new System.Windows.Forms.ComboBox();
            this.combo_GundamSeason4 = new System.Windows.Forms.ComboBox();
            this.combo_GundamSeason5 = new System.Windows.Forms.ComboBox();
            this.combo_GundamSeason6 = new System.Windows.Forms.ComboBox();
            this.btnUpdateBoxes = new System.Windows.Forms.Button();
            this.txtOptions = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.btnSend = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            this.listviewOptions = new System.Windows.Forms.ListView();
            this.comboSent6 = new System.Windows.Forms.ComboBox();
            this.comboSent5 = new System.Windows.Forms.ComboBox();
            this.comboSent4 = new System.Windows.Forms.ComboBox();
            this.comboSent3 = new System.Windows.Forms.ComboBox();
            this.comboSent2 = new System.Windows.Forms.ComboBox();
            this.comboSent1 = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();

            this.combo_GundamSeason1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.combo_GundamSeason1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.combo_GundamSeason1.FormattingEnabled = true;
            this.combo_GundamSeason1.Location = new System.Drawing.Point(43, 34);
            this.combo_GundamSeason1.Name = "combo_GundamSeason1";
            this.combo_GundamSeason1.Size = new System.Drawing.Size(261, 28);
            this.combo_GundamSeason1.TabIndex = 18;
            
            this.combo_GundamSeason2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.combo_GundamSeason2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.combo_GundamSeason2.FormattingEnabled = true;
            this.combo_GundamSeason2.Location = new System.Drawing.Point(43, 77);
            this.combo_GundamSeason2.Name = "combo_GundamSeason2";
            this.combo_GundamSeason2.Size = new System.Drawing.Size(261, 28);
            this.combo_GundamSeason2.TabIndex = 18;
           
            this.combo_GundamSeason3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.combo_GundamSeason3.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.combo_GundamSeason3.FormattingEnabled = true;
            this.combo_GundamSeason3.Location = new System.Drawing.Point(43, 120);
            this.combo_GundamSeason3.Name = "combo_GundamSeason3";
            this.combo_GundamSeason3.Size = new System.Drawing.Size(261, 28);
            this.combo_GundamSeason3.TabIndex = 18;
            
            this.combo_GundamSeason4.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.combo_GundamSeason4.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.combo_GundamSeason4.FormattingEnabled = true;
            this.combo_GundamSeason4.Location = new System.Drawing.Point(43, 159);
            this.combo_GundamSeason4.Name = "combo_GundamSeason4";
            this.combo_GundamSeason4.Size = new System.Drawing.Size(261, 28);
            this.combo_GundamSeason4.TabIndex = 18;
            
            this.combo_GundamSeason5.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.combo_GundamSeason5.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.combo_GundamSeason5.FormattingEnabled = true;
            this.combo_GundamSeason5.Location = new System.Drawing.Point(43, 202);
            this.combo_GundamSeason5.Name = "combo_GundamSeason5";
            this.combo_GundamSeason5.Size = new System.Drawing.Size(261, 28);
            this.combo_GundamSeason5.TabIndex = 18;
            
            this.combo_GundamSeason6.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.combo_GundamSeason6.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.combo_GundamSeason6.FormattingEnabled = true;
            this.combo_GundamSeason6.Location = new System.Drawing.Point(43, 248);
            this.combo_GundamSeason6.Name = "combo_GundamSeason6";
            this.combo_GundamSeason6.Size = new System.Drawing.Size(261, 28);
            this.combo_GundamSeason6.TabIndex = 18;
          
            this.btnUpdateBoxes.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.btnUpdateBoxes.Location = new System.Drawing.Point(43, 426);
            this.btnUpdateBoxes.Name = "btnUpdateBoxes";
            this.btnUpdateBoxes.Size = new System.Drawing.Size(261, 84);
            this.btnUpdateBoxes.TabIndex = 19;
            this.btnUpdateBoxes.Text = "Update ComboBoxes";
            this.btnUpdateBoxes.UseVisualStyleBackColor = true;
            this.btnUpdateBoxes.Click += new System.EventHandler(this.btnUpdateBoxes_Click);
            
            this.txtOptions.Location = new System.Drawing.Point(43, 289);
            this.txtOptions.Multiline = true;
            this.txtOptions.Name = "txtOptions";
            this.txtOptions.Size = new System.Drawing.Size(261, 131);
            this.txtOptions.TabIndex = 20;
            
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F);
            this.label1.Location = new System.Drawing.Point(49, 403);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(244, 17);
            this.label1.TabIndex = 21;
            this.label1.Text = "Enter Items  Separated with a Comma";
            
            this.btnSend.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.btnSend.Location = new System.Drawing.Point(558, 426);
            this.btnSend.Name = "btnSend";
            this.btnSend.Size = new System.Drawing.Size(207, 84);
            this.btnSend.TabIndex = 22;
            this.btnSend.Text = "Send Options in this Order";
            this.btnSend.UseVisualStyleBackColor = true;
            
            this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.button1.Location = new System.Drawing.Point(327, 426);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(207, 84);
            this.button1.TabIndex = 22;
            this.button1.Text = "Create List from ComboBoxes";
            this.button1.UseVisualStyleBackColor = true;
            
            this.listviewOptions.Location = new System.Drawing.Point(327, 25);
            this.listviewOptions.Name = "listviewOptions";
            this.listviewOptions.Size = new System.Drawing.Size(207, 395);
            this.listviewOptions.TabIndex = 23;
            this.listviewOptions.UseCompatibleStateImageBehavior = false;
            
            this.comboSent6.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboSent6.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.comboSent6.FormattingEnabled = true;
            this.comboSent6.Location = new System.Drawing.Point(558, 248);
            this.comboSent6.Name = "comboSent6";
            this.comboSent6.Size = new System.Drawing.Size(207, 28);
            this.comboSent6.TabIndex = 18;
           
            this.comboSent5.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboSent5.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.comboSent5.FormattingEnabled = true;
            this.comboSent5.Location = new System.Drawing.Point(558, 202);
            this.comboSent5.Name = "comboSent5";
            this.comboSent5.Size = new System.Drawing.Size(207, 28);
            this.comboSent5.TabIndex = 18;
                        this.comboSent4.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboSent4.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.comboSent4.FormattingEnabled = true;
            this.comboSent4.Location = new System.Drawing.Point(558, 159);
            this.comboSent4.Name = "comboSent4";
            this.comboSent4.Size = new System.Drawing.Size(207, 28);
            this.comboSent4.TabIndex = 18;
            
            this.comboSent3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboSent3.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.comboSent3.FormattingEnabled = true;
            this.comboSent3.Location = new System.Drawing.Point(558, 120);
            this.comboSent3.Name = "comboSent3";
            this.comboSent3.Size = new System.Drawing.Size(207, 28);
            this.comboSent3.TabIndex = 18;
            
            this.comboSent2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboSent2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.comboSent2.FormattingEnabled = true;
            this.comboSent2.Location = new System.Drawing.Point(558, 77);
            this.comboSent2.Name = "comboSent2";
            this.comboSent2.Size = new System.Drawing.Size(207, 28);
            this.comboSent2.TabIndex = 18;
            
            this.comboSent1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboSent1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.comboSent1.FormattingEnabled = true;
            this.comboSent1.Location = new System.Drawing.Point(558, 34);
            this.comboSent1.Name = "comboSent1";
            this.comboSent1.Size = new System.Drawing.Size(207, 28);
            this.comboSent1.TabIndex = 18;
           
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(865, 669);
            this.Controls.Add(this.listviewOptions);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.btnSend);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtOptions);
            this.Controls.Add(this.btnUpdateBoxes);
            this.Controls.Add(this.comboSent1);
            this.Controls.Add(this.comboSent2);
            this.Controls.Add(this.combo_GundamSeason1);
            this.Controls.Add(this.comboSent3);
            this.Controls.Add(this.combo_GundamSeason2);
            this.Controls.Add(this.comboSent4);
            this.Controls.Add(this.combo_GundamSeason3);
            this.Controls.Add(this.comboSent5);
            this.Controls.Add(this.combo_GundamSeason4);
            this.Controls.Add(this.comboSent6);
            this.Controls.Add(this.combo_GundamSeason5);
            this.Controls.Add(this.combo_GundamSeason6);
            this.Name = "Form1";
            this.Text = "Code Elucidate: Dynamically Populate and Retrieve ComboBox Items";
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        #endregion

        private System.Windows.Forms.ComboBox combo_GundamSeason1;
        private System.Windows.Forms.ComboBox combo_GundamSeason2;
        private System.Windows.Forms.ComboBox combo_GundamSeason3;
        private System.Windows.Forms.ComboBox combo_GundamSeason4;
        private System.Windows.Forms.ComboBox combo_GundamSeason5;
        private System.Windows.Forms.ComboBox combo_GundamSeason6;
        private System.Windows.Forms.Button btnUpdateBoxes;
        private System.Windows.Forms.TextBox txtOptions;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button btnSend;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.ListView listviewOptions;
        private System.Windows.Forms.ComboBox comboSent6;
        private System.Windows.Forms.ComboBox comboSent5;
        private System.Windows.Forms.ComboBox comboSent4;
        private System.Windows.Forms.ComboBox comboSent3;
        private System.Windows.Forms.ComboBox comboSent2;
        private System.Windows.Forms.ComboBox comboSent1;
2. Add code to Form1.cs to process data user-updated UI data -place code within declaration for

public partial class Form1 : Form
        IDictionary options_dict;

        public Form1()
        {
            InitializeComponent();
            options_dict = new Dictionary();

            //POPULATE THE COMBOBOXES WITH PREDEFINED ITEM LIST
            setComboBoxes(getComboBoxes(), getGundamList());
        }

        private void btnUpdateBoxes_Click(object sender, EventArgs e)
        {
            updateComboValues();
        }
        private void btnGetGundamList_Click(object sender, EventArgs e)
        {
            setComboBoxes(getComboBoxes(), getGundamList());
        }

        private void updateComboValues()
        {
            /* INSTANTIATE DICTIONARY TO HOLD THE VALUES*/
            options_dict = new Dictionary();

            /* GET THE VALUES ENTERED IN THE TEXT BOX IN A LIST*/
            List txtOptions = getTxtOptions();

            /* PUT THE VALUES LIST INTO THE GLOBAL DICTIONARY*/
            fillDictionary(txtOptions);

            /* GET THE COMBOBOXES, REMOVE THEIR EXISTING ITEMS
                THEN POPULATE THEM WITH THE NEW DICTIONARY VALUES
             */
            ComboBox[] comboBoxes = getComboBoxes();
            clearComboitems(comboBoxes);
            populateComboBoxes(comboBoxes, options_dict);
        }

        private void clearComboitems(ComboBox[] boxes)
        {
            foreach (ComboBox box in boxes)
            {
                box.Items.Clear();
            }
        }
        private ComboBox[] getComboBoxes()
        {
            return new ComboBox[]
            {
                combo_GundamSeason1,
                combo_GundamSeason2,
                combo_GundamSeason3,
                combo_GundamSeason4,
                combo_GundamSeason5,
                combo_GundamSeason6
            };

        }

        private void populateComboBoxes(ComboBox[] comboBoxes, IDictionary options)
        {
            int count = 0;
            foreach (ComboBox dropdown in comboBoxes)
            {
                foreach(KeyValuePair kvp in options)
                {
                    dropdown.Items.Add(kvp.Value);
                }
                dropdown.SelectedIndex = count;
                count++;
            }
        }

        private List getTxtOptions()
        {
            string[] optionSplit = txtOptions.Text.Split(new char[]{','});
            List optionList = new List();

            int count = 0;
            foreach(string op in optionSplit)
            {
                optionList.Add(op);
                count++;
            }
            return optionList;
        }

        private List getGundamList()
        {
            string gundam = "Gundam ";
            return new List
            {
                gundam + "Seed",
                gundam + "00",
                gundam + "Wing",
                gundam + "ZZ",
                gundam + "X",
                gundam + "Victory"
            };
        }
        private void setComboBoxes(ComboBox[] comboBoxes, List gundamList)
        {
            clearComboitems(comboBoxes);

            int count = 0;
            foreach (ComboBox box in comboBoxes)
            {
                foreach (string gundam in gundamList)
                {
                    box.Items.Add(gundam);
                }
                box.SelectedIndex = count;
                count++;
            }
        }

        private void fillDictionary(List list)
        {
            //PUT VALUES INTO DICTIONARY
            int count = 0;
            foreach (string item in list)
            {
                options_dict.Add(count, item);
                count++;
            }
        }

        private void clearListView(ListView listview)
        {
            listview.Clear();
        }

        private void btnCreateListFromComboBoxes_Click(object sender, EventArgs e)
        {
            Dictionary map_values_to_combos
                = getComboBoxValues(getComboBoxes());

            /* CLEAR LIST VIEW THEN FILL WITH SELECTED COMBOBOX VALUES*/
            clearListView(this.listviewOptions);
            fillListView(map_values_to_combos);

        }
        private Dictionary getComboBoxValues(ComboBox[] boxes)
        {
            Dictionary box_dict = new Dictionary();
            int count = 0;
            foreach (ComboBox box in boxes)
            {
                box_dict.Add(count, box.SelectedItem);
                count++;
            }
            return box_dict;
        }
        private void fillListView(Dictionary map)
        {
            foreach (KeyValuePair kvp in map)
            {
                listviewOptions.Items.Add(new ListViewItem((string)kvp.Value));
            }
        }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: