Auto Find and display objects when input keywords in VB

自古美人都是妖i 提交于 2019-12-24 08:34:34

问题


I'm a newbie in VB form programming. I got a task today, it seems so hard for me. basically, I have 2 forms named: Form1 and Form2. Form1 use for management info of students such as: student's code, name, phone number, etc..i have a button in Form1, when I press it, Form2 will be invoked. i have completed it.

the problem is: in Form2, I just have 1 textbox and 1 listbox. how should it work? => when I type in textbox with 1 keyword relate to info of students in Form1, Listbox will display automatically all of students's name that relate to the keyword I typed . I didn't know any idea to do it automatically. anyone can help me please give me a way. Thanks you so much.


回答1:


Based on the solution which you use to store list of students there could be different possible solutions. But for all of them, using TextChanged event of that TextBox you can set the filter for the main list which contains students.

If you use a DataTable to store students, you can use DefaultView.RowFilter of the DataTable:

dt.DefaultView.RowFilter = string.Format("Name Like '{0}*'", TextBox1.Text)

Also if you are using generic lists for storing students, you can use linq to do so:

var data = list.Where(Function(s) s.Name.StartsWith(x.Name.StartsWith(TextBox1.Text)) _
               .ToList()
listBox1.DataSource = New BindingList(Of Student)(data)

Also if interaction between forms was an issue for you, take a look at this post.



来源:https://stackoverflow.com/questions/40279675/auto-find-and-display-objects-when-input-keywords-in-vb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!