Resolved Customized combobox

Ismael

Member
Joined
Mar 20, 2020
Messages
18
Programming Experience
1-3
Hello. I have a form with a combobox that is populated with client names. But to insert a record ina a table I use the client code. What I want to do is create a special combobox with an extra property called Cods, which will be populated at the same time that ComboBox Items are populated with the names. So, when I select a user name, then the Combobox.SelectedIndex can be used to sinchronize names and codes for the combobox. I have started creating this user control:
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.ComboBox;

namespace Tools
{
    public partial class BdComboBox : UserControl
    {
        public BdComboBox()
        {
            InitializeComponent();
        }
        public ObjectCollection Cods { get; set; }
        public void Sincroniza()
        {
            int index = this.comboBox1.SelectedIndex;
        }
    }
}
What I don't know how to do is how to use variable index to get the correspondig valuein property Cods. Can anybody help me?
Thanks.
 
If I'm understanding you correctly, the standard ComboBox control can already do what you want. You just need to bind it appropriately. This is often done with a DataTable populated from a database but it can be done with an array or collection too. Here's an example using a DataTable:
C#:
var table = new DataTable();

using (var adapter = new SqlDataAdapter("SELECT PersonId, FullName FROM Person", "connection string here"))
{
    adapter.Fill(table);
}

personComboBox.DisplayMember = "FullName";
personComboBox.ValueMember = "PersonId";
personComboBox.DataSource = table;
The DisplayMember specifies the property or column to get the values from to display in the control. The ValueMember specifies the property or column to get values from to expose via the SelectedValue. After running that code, the user can select a FullName from the list and you can get the corresponding PersonId from the SelectedValue of the ComboBox. You can also assign a PersonId value to the SelectedValue to select the corresponding FullName in the list. This mechanism is used in combo box columns in a DataGridView, where IDs are passed between the Value of a cell and the SelectedValue of an editing control. You can do the same thing with a list of items with properties, e.g.
C#:
var people = new List<Person>
                {
                    new Person {PersonId = 1, FullName = "Peter Smith"},
                    new Person {PersonId = 2, FullName = "Paul Jones"},
                    new Person {PersonId = 3, FullName = "Mary Anderson"},
                };

personComboBox.DisplayMember = "FullName";
personComboBox.ValueMember = "PersonId";
personComboBox.DataSource = people;
 
By the way, if you were going to create a custom ComboBox, you would inherit ComboBox, not UserControl. A user control is for when you want to create a group of child controls that you can use as a unit, e.g. a group of Labels and TextBoxes that can display and edit multiple fields for a single record. If you just want to change the behaviour of an existing control without changing the UI, you just inherit that control and add the appropriate code. That might be adding properties or overriding methods.
 
Back
Top Bottom