[dba-VB] C# combo boxes

Gustav Brock Gustav at cactus.dk
Mon Mar 14 14:02:27 CDT 2011


Hi John

If you (typically) have this in InitializeComponent() of the .Designer.cs file:

            // comboBoxCountry
            // 
            this.comboBoxCountry.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
            this.comboBoxCountry.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
            this.comboBoxCountry.BackColor = System.Drawing.Color.LavenderBlush;
            this.comboBoxCountry.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.mediaBindingSource, "CountryId", true));
            this.comboBoxCountry.DisplayMember = "Id";
            this.comboBoxCountry.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.comboBoxCountry.Font = new System.Drawing.Font("Segoe UI", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.comboBoxCountry.ForeColor = System.Drawing.Color.DarkMagenta;
            this.comboBoxCountry.FormattingEnabled = true;
            this.comboBoxCountry.ItemHeight = 13;
            this.comboBoxCountry.Location = new System.Drawing.Point(21, 136);
            this.comboBoxCountry.Name = "comboBoxCountry";
            this.comboBoxCountry.Size = new System.Drawing.Size(117, 21);
            this.comboBoxCountry.Sorted = true;
            this.comboBoxCountry.TabIndex = 7;
            this.comboBoxCountry.ValueMember = "Id";

You can do something like this in the Load event:

            this.comboBoxCountry.SelectedValueChanged += (oo, ee) => ChangeCountrySelection();

Then:
        private void FillComboBoxCountryCode()
        {
            // Order the country names of countryDataTable.
            string filterExpression = "";
            string sortExpression = "CountryNameUK asc";
            DataRow[] countryList = _countryDataTable.Select(filterExpression, sortExpression);

            this.comboBoxCountry.DataSource = countryList;
            this.comboBoxCountry.ValueMember = "Id";
            this.comboBoxCountry.DisplayMember = "CountryNameUK";
        }

And:
        private void ChangeCountrySelection()
        {
            if (this.comboBoxCountry.SelectedValue != null)
            {
                _countryId = Convert.ToInt32(this.comboBoxCountry.SelectedValue);
                this.comboBoxPostalCode.Text = String.Empty;
                // Read postal codes of selected country.
                FillComboBoxPostalCode();
            }
        }

This is pretty standard using tableadapters and bindingnavigator. Nothing fancy.

/gustav


>>> jwcolby at colbyconsulting.com 14-03-2011 19:16 >>>
I am trying to set up a combo box bound to a field in the form such that the combo populates with 
the PKID and a text field, and when the form opens, selecting something from the combo drops the 
PKID into the field in the form.

I am not getting what to set the properties of the combo box to.  I am pulling the ID and the string 
to display into the combo but I cannot cause the combo to store the id and release control to the 
next control on the form.
-- 
John W. Colby
www.ColbyConsulting.com 





More information about the dba-VB mailing list