🔒 Closed Search and Remove Item in Listbox with LINQ [Tutorial 2]

Status
Not open for further replies.
Best method to search and remove item in listbox

quick demo:
download


CODE:

C#:
private void SearchAndRemoveItems(string searchString)
{
    listBox1.BeginUpdate();
    var item = listBox1.Items.Cast<string>().ToList().Where(x => x.Contains(searchString)).ToList(); //you can change x.Contains to x.Equals if you want specific and exact words only
    item.ForEach(listBox1.Items.Remove);
    item.ForEach(x => lblResult.Text = $"{x} has been removed");
    listBox1.EndUpdate();

    CountListBoxItems();
    textBox1.SelectAll();
}

private void CountListBoxItems()
{
     lblCount.Text = listBox1.Items.Count.ToString();
}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter) {
    SearchAndRemoveItems(textBox1.Text);
    }
}
 
Status
Not open for further replies.

About this Thread

  • 0
    Replies
  • 187
    Views
  • 1
    Participants
Last reply from:
zackmark29

Online now

Members online
921
Guests online
3,307
Total visitors
4,228

Forum statistics

Threads
2,276,129
Posts
28,967,781
Members
1,231,127
Latest member
zrhu
Back
Top