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

k5hmc34c  于 2023-01-21  发布在  其他
关注(0)|答案(5)|浏览(135)

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

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

但是我找不到这样的东西,我还以为遍历它们可以找到它呢?所以我得到了

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

却在行中找不到值成员
谢谢

b4wnujal

b4wnujal1#

好的,这里有一个简单的例子,但我想这是主要的想法,我们有一个MyClass,它有Id作为值成员,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:在示例中,我假设将在文本框中键入整数

n6lpvg4x

n6lpvg4x2#

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

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

w6lpcovy3#

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.
sigwle7e

sigwle7e4#

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

jvidinwx5#

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

相关问题