开发者

Search a ListBox and Select result in C#

I have searched the site but could not find a answer.

I have a listbox called "CompetitorDetailsOutput" I then have a textbox above called "searchbox" and a button called "searchbutton" The data in the list box constanly changes and get it data from a .txt file wich stores the 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 listbox then displays as follows

string.Format("{0,-20}|{1,-10}|{2,-9}|{3,-7}|{4,2}|{5,2}|{6,2}|{7,2}|{8,2}|开发者_如何学C{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 the listbox as follows: user only enters data into "searchbox" and presses "searchbutton", system then searches the listbox, If it finds it selects the item in the listbox, if not then a close match is selected, if there are no close matches then a error message is displayed.

Code is C# and software VS 2008 Pro

Thanks


Try something like this to get your 'match' algorithm started:

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


1./ Create an object with the properties that you want to search on
2./ Add your items as an object rather than as a string
3./ override the ToString() with the format that you want to display in the listbox
4./ Use Linq to query your objects as you like.

var result = from o in ListBox.Items.OfType<yourClass>()
             where o.Whatever == yourCriteria
             select o;


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜