如何确定组合框是否包含值成员?

时间:2012-12-07 12:02:50

标签: c# winforms combobox

情况是我有2个控件,一个文本框和一个组合框。用户可以在组合框中选择一些东西,它用值成员填充文本框,如果用户键入文本框,我想检查它是否存在于组合框的值中,然后选择相应的显示成员。 / p>

我期待的方法就像

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

但我找不到这样的东西,我还以为通过它们循环可以找到它?所以我有

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

但在行中的任何地方找不到值成员?

由于

5 个答案:

答案 0 :(得分:2)

好的,这是一个简单的例子,但我猜这是主要的想法。我们有一个MyClass,其中ValueMember为Id,DisplayMember为Name

 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();
    }
}

随意询问某些事情是否不清楚。

PS:在示例中,我假设整数将在文本框中输入

答案 1 :(得分:2)

游戏有点晚了,但是我找不到任何有用的东西,所以我想出了一个简单的解决方案:

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

或者:

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

示例来演示:

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);

或者,如果您需要获取商品的索引,则可以使用以下内容:

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

答案 2 :(得分:0)

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.

答案 3 :(得分:-1)

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

答案 4 :(得分:-1)

    private void binging(ComboBox cbo, string sql) {
        SqlDataAdapter da = new SqlDataAdapter();
        if (Utils.variable.con.State == ConnectionState.Closed) Utils.variable.con.Open();
        da.SelectCommand = new SqlCommand(sql, Utils.variable.con);
        DataSet ds = new DataSet();
        da.Fill(ds, "tb");
        cbo.DataSource = ds.Tables["tb"];
        cbo.ValueMember = "ID";
        cbo.DisplayMember = "Name";
        Utils.variable.con.Close();
    }

    private void frmSaleOrder_WH_Load(object sender, EventArgs e)
    {
        binging(cboItem, "Select ItemCode as ID,Des1 as Name from tb_IC_Item");
        binging(cboLocation, "Select SubLocationID as ID,Address1 as Name From tb_IC_SubLocation Where WHID Like '"+ ((cboWH.SelectedValue==null) ? "%" : cboWH.SelectedValue) +"'");
        binging(cboLocation, "");
        binging(cboLocation, "");
    }

试试这个