1

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.

  • Welcome to [so], but I'm sorry we don't provide code writing service. We can help if you can provide your code and what problem you have. – Prisoner Oct 27 '16 at 08:28
  • what do you mean? i have described my problem above :) – Giải Trí Cho Bé Oct 27 '16 at 08:31
  • 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 set `dt.DefaultView.RowFilter = string.Format("Name Like '{0}*'", Text)`. Also if you are using generic lists for storing students, you can use linq to do so. – Reza Aghaei Oct 27 '16 at 08:31
  • @RezaAghaei wow. an idea just raised on me. Thank you so much!!! – Giải Trí Cho Bé Oct 27 '16 at 08:38
  • @RezaAghaei i use blindinglist to store list of students. how can i use linq with it? so sorry :( – Giải Trí Cho Bé Oct 27 '16 at 08:44
  • JFYI, You can accept an answer by click on check mark near the answer. Also when you accept an answer, it would be great if you also vote for the answer by click on up arrow near the post. It's not compulsory at all, but it's common, reasonable and recommended. For more information about how does accepting answers work see this [post](http://meta.stackexchange.com/questions/23138/how-to-accept-the-answer-on-stack-overflow). – Reza Aghaei Nov 12 '16 at 01:29

1 Answers1

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.

Community
  • 1
  • 1
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398