- Joined
- Mar 25, 2013
- Posts
- 4,237
- Solutions
- 23
- Reaction
- 10,640
- Points
- 2,297
Dahil may nakita akong question about searching in listbox, so I made a simple LINQ code which is easy to undestand and use
Quick demo:
Here's the code:
Quick demo:
Here's the code:
C#:
private void button1_Click(object sender, EventArgs e)
{
SearchItems(textBox1.Text);
}
//Enter keypress
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
SearchItems(textBox1.Text);
}
}
//Search function
private void SearchItems(string searchString)
{
var i = listBox1.Items.Cast<string>().ToList()
.Where(item => item.Contains(searchString)).ToList();
//or item.Equals if you want specific words only
i.ForEach(x => listBox1.SetSelected(listBox1.Items.IndexOf(x), true));
i.ForEach(x => textBox2.Text = x.ToString());
textBox1.SelectAll();
}
Last edited: