Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how List<string> become AutoCompleteStringCollection

I have list, i want to convert it to autoCompleteStringCollection.. And I don't want use foreach.

        _textbox.AutoCompleteMode = AutoCompleteMode.Append;
        _textbox.AutoCompleteSource = AutoCompleteSource.CustomSource;
        _textbox.AutoCompleteCustomSource = user.GetNameUsers() as AutoCompleteStringCollection;

Note user.GetNameUsers() is list.

Code doesn't work, it become null.

Thank you

like image 900
aeruL Avatar asked Jan 13 '11 08:01

aeruL


1 Answers

_textbox.AutoCompleteMode = AutoCompleteMode.Append;
_textbox.AutoCompleteSource = AutoCompleteSource.CustomSource;
var autoComplete = new AutoCompleteStringCollection();
autoComplete.AddRange(user.GetNameUsers().ToArray());
_textbox.AutoCompleteCustomSource = autoComplete;

If you need this often, you can write an extension method:

public static class EnumerableExtensionsEx
{
    public static AutoCompleteStringCollection ToAutoCompleteStringCollection(
        this IEnumerable<string> enumerable)
    {
        if(enumerable == null) throw new ArgumentNullException("enumerable");
        var autoComplete = new AutoCompleteStringCollection();
        foreach(var item in enumerable) autoComplete.Add(item);
        return autoComplete;
    }
}

Usage:

_textbox.AutoCompleteCustomSource = user.GetUsers().ToAutoCompleteStringCollection();
like image 150
max Avatar answered Oct 04 '22 00:10

max