Search list for item containing string

Background . I am creating a database application to store information about my massive movie collection. The list contains hundreds of elements, so I decided to implement a search function that selects all elements containing a specific string. Sometimes it’s hard to remember the whole title of a movie, so I thought it would come in handy.

I found this useful code on a Microsoft site that selects all the items in a list containing a specific string. How can I change it to search on each line as a whole?

Currently, the code searches for start elements using a search string instead of seeing if it contains a search string elsewhere. I came across the listbox.items.contains () method on Google, although I have no idea how to convert my code for it.

http://forums.asp.net/t/1094277.aspx/1

private void FindAllOfMyString(string searchString) { // Set the SelectionMode property of the ListBox to select multiple items. listBox1.SelectionMode = SelectionMode.MultiExtended; // Set our intial index variable to -1. int x =-1; // If the search string is empty exit. if (searchString.Length != 0) { // Loop through and find each item that matches the search string. do { // Retrieve the item based on the previous index found. Starts with -1 which searches start. x = listBox1.FindString(searchString, x); // If no item is found that matches exit. if (x != -1) { // Since the FindString loops infinitely, determine if we found first item again and exit. if (listBox1.SelectedIndices.Count > 0) { if(x == listBox1.SelectedIndices[0]) return; } // Select the item in the ListBox once it is found. listBox1.SetSelected(x,true); } }while(x != -1); } } 
+4
source share
6 answers

Create your own search function, something like strings

 int FindMyStringInList(ListBox lb,string searchString,int startIndex) { for(int i=startIndex;i<lb.Items.Count;++i) { string lbString = lb.Items[i].ToString(); if(lbString.Contains(searchString)) return i; } return -1; } 

(beware, I wrote this from my head without compilation or testing, the code may contain errors, but I think you will get the idea !!!)

+4
source

Use String.IndexOf for a case-sensitive string search in each item in a ListBox:

 private void FindAllOfMyString(string searchString) { for (int i = 0; i < listBox1.Items.Count; i++) { if (listBox1.Items[i].ToString().IndexOf(searchString, StringComparison.OrdinalIgnoreCase) >= 0) { listBox1.SetSelected(i, true); } else { // Do this if you want to select in the ListBox only the results of the latest search. listBox1.SetSelected(i, false); } } } 

I also suggest that you set the ListBox SelectionMode property in the Winform constructor or in the form constructor method.

0
source

I'm not sure about the code you posted, but I wrote a small method to do what you are looking for. It is very simple:

  private void button1_Click(object sender, EventArgs e) { listBox1.SelectionMode = SelectionMode.MultiSimple; IEnumerable items = listBox1.Items; List<int> indices = new List<int>(); foreach (var item in items) { string movieName = item as string; if ((!string.IsNullOrEmpty(movieName)) && movieName.Contains(searchString)) { indices.Add(listBox1.Items.IndexOf(item)); } } indices.ForEach(index => listBox1.SelectedIndices.Add(index)); } 
0
source

How about below. You just need to improve it a bit.

  using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public class Item { public Item(string id,string desc) { id = id; Desc = desc; } public string Id { get; set; } public string Desc { get; set; } } public partial class Form1 : Form { public const string Searchtext="o"; public Form1() { InitializeComponent(); listBox1.SelectionMode = SelectionMode.MultiExtended; } public static List<Item> GetItems() { return new List<Item>() { new Item("1","One"), new Item("2","Two"), new Item("3","Three"), new Item("4","Four") }; } private void Form1_Load(object sender, EventArgs e) { listBox1.DataSource = GetItems(); listBox1.DisplayMember = "Desc"; listBox1.ValueMember = "Id"; listBox1.ClearSelected(); listBox1.Items.Cast<Item>() .ToList() .Where(x => x.Desc.Contains("o")).ToList() .ForEach(x => listBox1.SetSelected(listBox1.FindString(x.Desc),true)); } } } 
0
source

The same question was asked: Find ListBox and Select result in C #

I suggest another solution:

  • Not suitable for list> 1000 items.

  • Each iteration combines all the elements.

  • Finds and stays in the most suitable case.

     // Save last successful match. private int lastMatch = 0; // textBoxSearch - where the user inputs his search text // listBoxSelect - where we searching for text private void textBoxSearch_TextChanged(object sender, EventArgs e) { // Index variable. int x = 0; // Find string or part of it. string match = textBoxSearch.Text; // Examine text box length if (textBoxSearch.Text.Length != 0) { bool found = true; while (found) { if (listBoxSelect.Items.Count == x) { listBoxSelect.SetSelected(lastMatch, true); found = false; } else { listBoxSelect.SetSelected(x, true); match = listBoxSelect.SelectedValue.ToString(); if (match.Contains(textBoxSearch.Text)) { lastMatch = x; found = false; } x++; } } } } 

Hope this help!

0
source

only one liner

  if(LISTBOX.Items.Contains(LISTBOX.Items.FindByText("anystring"))) //string found else //string not found 

:) Yours faithfully

-1
source

All Articles