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); } }
source share