Alex
Alex

Reputation: 828

How can I find out if a combo box contains a value member?

The situation is that I have 2 controls, a text box and a combo box. The user can select something in the combo box, it fills the text box with the value member, if the user types into the text box, I want to check if it exists in the combo box's values and then select the corresponding display member.

The method I was expecting to be there was something like

if(cmb1.valueMembers.Contains(txt1.Text))

but I can't find anything like this, I also thought looping through them could find it? so I've got

foreach (System.Data.DataRowView row in cmb1.Items)
        {}

but can't find the value member anywhere in the row?

Thanks

Upvotes: 2

Views: 16547

Answers (5)

Billy Xd
Billy Xd

Reputation: 89

        bool _found = false;
        string _txt = comboBox1.Text;
        foreach (var row in comboBox1.Items)
        {
            if (_txt == row.ToString()) { _found = true; }
        }

Upvotes: 0

Cristian Ariel Ab
Cristian Ariel Ab

Reputation: 1

ListSubCategoryProduct = await Task.Run<List<SubCategoryProduct>>(() => { 
    return productsController.ListSubCategoryProduct(CategoryProductId); }); // <- I search the database

                CboSubCategory.DataSource = ListSubCategoryProduct;
                CboSubCategory.DisplayMember = "Description";
                CboSubCategory.ValueMember = "SubCategoryProductId";

                CboSubCategory.AutoCompleteMode = AutoCompleteMode.Suggest;
                CboSubCategory.AutoCompleteSource = AutoCompleteSource.ListItems;

                this.CboSubCategory.SelectedValue = 1; // <- SubCategoryProductId. You have to know the ID.

Upvotes: 0

41686d6564
41686d6564

Reputation: 19661

A bit late to the game but I couldn't find anything useful so I came up with this simple solution:

comboBox1.Items.OfType<SomeType>().Any(x => x == YourValue)

Or:

comboBox1.Items.OfType<SomeType>().Any(x => x.SomeProperty == YourValue)

Example to demonstrate:

class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

// ...

var people = new List<Person>() { /* Add some data */ };
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";
comboBox1.DataSource = people;

// ...

bool exists = comboBox1.Items.OfType<Person>().Any(p => p.Id == 1);

Or if you need to get the index of the item, you can use something like this:

var person = comboBox1.Items.OfType<Person>().FirstOrDefault(p => p.Id == 1);
var index = (person != null) ? comboBox1.Items.IndexOf(person) : -1;

Upvotes: 2

Waleed Ehsan
Waleed Ehsan

Reputation: 19

Private Sub ComboBox1_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
    If ComboBox1.SelectedIndex = -1 Then              
        Return
    Else
        TextBox1.Text = ComboBox1.SelectedValue.ToString   ' if find then show their displaymember in combobox.
    End If


Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
    Dim value As String = TextBox1.Text
    ComboBox1.SelectedValue = value                                    ' if find then show their displaymember in combobox.

    If ComboBox1.SelectedValue Is Nothing Then                          ' if the id you entered in textbox is not find.
        TextBox1.Text = String.Empty

    End If

Upvotes: -1

Dimitar Tsonev
Dimitar Tsonev

Reputation: 3892

Ok, here's a simple example but I guess that's the main idea. We have a MyClass which have Id for the ValueMember and Name for the DisplayMember.

 public partial class Form1 : Form
{
    class MyClass
    {
        public MyClass(string name, int id)
        {
            Name = name;
            Id = id;
        }
        public string Name { get; set; }
        public int Id { get; set; }
    }

    List<MyClass> dsList = new List<MyClass>();

    public Form1()
    {

        for (int i = 0; i < 10; i++)
        {
            dsList.Add(new MyClass("Name" + i , i));
        }

        InitializeComponent();

        comboBox1.DataSource = dsList;
        comboBox1.ValueMember = "Id";
        comboBox1.DisplayMember = "Name";
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        //Checks if item with the typed Id exists in the DataSource
        // and selects it if it's true
        int typedId = Convert.ToInt32(textBox1.Text);
        bool exist = dsList.Exists(obj => obj.Id == typedId);
        if (exist) comboBox1.SelectedValue = typedId;

    }


    private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
    {
        MyClass obj = comboBox1.SelectedValue as MyClass;
        if (obj != null) textBox1.Text = obj.Id.ToString();
    }
}

Feel free to ask if something's not clear.

PS: In the example I'm assuming that integers will be typed in the textbox

Upvotes: 3

Related Questions