首页 > 解决方案 > 如何在文本框中返回匹配值?

问题描述

我有一个由 csv 填充的组合框。在我的 csv 中,我有 3 列,第一列是 id #s。

标签: vb.netcsv

解决方案


也许你可以试试ValueMember

这是一个例子:

public partial class Form1 : Form
{
    List<Student> listStudents = new List<Student>();
    public Form1()
    {
        InitializeComponent();
        listStudents.Add(new Student(101, "name1", "male"));
        listStudents.Add(new Student(102, "name2", "female"));
        listStudents.Add(new Student(103, "name3", "female"));
        listStudents.Add(new Student(104, "name4", "male"));

        var maleStudentList = listStudents.Where(student => student.gender == "male").ToList();

        comboBox1.DisplayMember = "name";
        comboBox1.ValueMember = "id";
        comboBox1.DataSource = maleStudentList;
    }
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        label1.Text = ((Student)comboBox1.SelectedItem).id.ToString();
    }
}
class Student
{
    public int id { get; set; }
    public string name { get; set; }
    public string gender { get; set; }

    public Student(int id, String name, String gender)
    {
        this.id = id;
        this.name = name;
        this.gender = gender;
    }
}

来源:https ://social.msdn.microsoft.com/Forums/windowsmobile/en-US/1a978579-1938-44cd-aab3-d1964548a814/why-dont-comboboxs-item-have-a-tag-property?forum= csharpgeneral


推荐阅读