Find ListBox and select result in C #

I searched the site but could not find the answer.

I have a list called "CompetitorDetailsOutput". Then I have a text box called "search query" and a "searchbutton" button. The data in the list box is constantly changing and receiving data from a TXT file that stores data in the following format

string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12}", Name, CPSA, PostCode, Rank, Score1, Score2, Score3, Score4, Score5, Score6, Score7, Score8, TotalSingleScore); 

the list is then displayed as follows

 string.Format("{0,-20}|{1,-10}|{2,-9}|{3,-7}|{4,2}|{5,2}|{6,2}|{7,2}|{8,2}|{9,2}|{10,2}|{11,2}|{12,3}", Name, CPSA, PostCode, Rank, Score1, Score2, Score3, Score4, Score5, Score6, Score7, Score8, TotalSingleScore) 

I want to be able to search in the list: the user enters data only in the "search" and clicks "searchbutton", the system then searches in the list. If he finds that he selects an item in the list, if not, then a match is selected, if there are no close matches, then an error message appears.

Code is C # and VS 2008 Pro Software

thanks

+1
source share
3 answers

Try something similar to run the match algorithm:

 foreach (var item in ListBox.Items) { if (item.Text.Contains(searchArg)) { //select this item in the ListBox. ListBox.SelectedValue = item.Value; break; } } 
+4
source

1./Create an object with the properties you want to find on sale 2./Add your objects as an object, not as a string
3./ redefine ToString () with the format you want to list | 4./Use Linq to query your objects as you wish.

 var result = from o in ListBox.Items.OfType<yourClass>() where o.Whatever == yourCriteria select o; 
+1
source
 private void FindAllOfMyString(string searchString) { // Set the SelectionMode property of the ListBox to select multiple items. ListBox.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 = ListBox.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 (ListBox.SelectedIndices.Count > 0) { if (x == ListBox.SelectedIndices[0]) return; } // Select the item in the ListBox once it is found. ListBox.SetSelected(x, true); } } while (x != -1); } } private void Srchbtn_Click(object sender, EventArgs e) { FindAllOfMyString(SrchBox.Text); } 

http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.findstring(v=vs.71).aspx

+1
source

All Articles