vb.net - 如何在文本框中返回匹配值?
问题描述
我有一个由 csv 填充的组合框。在我的 csv 中,我有 3 列,第一列是 id #s。
解决方案
也许你可以试试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;
}
}
推荐阅读
- javascript - vue测试登录表单
- c# - C#:在 Excel 工作表中查找确切的字符串不适用于 xlWhole
- javascript - HTML2Canvas 未捕获在 IE 11 中呈现 SVG 的画布
- python - Pymongo 即使出现重复键错误也会插入 id
- flutter - Flutter/Dart - Navigator.pop(context) 在添加 BottomNavigationBar 后停止工作
- c# - 如何找到在 Windows 控件中放置黄色方块的代码所在的代码行?
- spring-data - 使用 spring-data-envers 查询嵌套对象的修订
- python - Matplotlib 挤压 x 标签
- javascript - Datatables 1.9.4: data-order attribute is ignored
- javascript - React useState setter 不会更新上下文中的值