问题
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