🔒 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
  • 203
    Views
  • 1
    Participants
Last reply from:
zackmark29

Trending Topics

Online now

Members online
968
Guests online
8,662
Total visitors
9,630

Forum statistics

Threads
2,354,364
Posts
29,383,396
Members
1,079,429
Latest member
edie wow
Back
Top