2

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

我期待的方法是这样的

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

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

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

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

谢谢

4

4 回答 4

3

好的,这是一个简单的例子,但我想这是主要思想。我们为 ValueMember 和DisplayMember 提供了一个MyClassIdName

 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:在示例中,我假设将在文本框中输入整数

于 2012-12-07T12:38:24.427 回答
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;
于 2019-03-02T23:17:41.260 回答
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.
于 2020-04-01T13:36:54.367 回答
-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
于 2013-05-22T02:21:19.433 回答