how List<string> become AutoCompleteStringCollection
Asked Answered
C

2

6

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

Cortneycorty answered 13/1, 2011 at 8:51 Comment(3)
It might be important to note: The reason you are getting null is because the as operator trys to coerce your List<string> into an AutoCompleteStringCollection, but as only succeeds if the runtime types match (it won't try any user defined conversions), and the runtime types of List<string> and AutoCompleteStringCollection are not the same, so it returns null instead.Euphonious
owww i got that, thank you . Make it more understandCortneycorty
this.textbox.AutoCompleteCustomSource.AddRange(new string[] {"anytext","another one"});Wadewadell
C
26
_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();
Crazy answered 13/1, 2011 at 8:55 Comment(2)
And, wrap it in a utility method if you are looking for a one-liner.Restitution
Make sure you strip out any null values form your query when doing this. I wasn't doing this and it would crash when setting AutoCompleteCustomSource with no error messages or dubugging information.Misjudge
S
2

Having checked the documentation for AutoCompleteStringCollection, and specifically the constructor I see there is no constructor which takes a List.

Therefore, you have 2 options.

1) Use AddRange to add all your list items to a new instance of AutoCompleteStringCollection

var acsc= new AutoCompleteStringCollection();
acsc.AddRange(user.GetNameUsers().ToArray());

2) Inherit a new class, which adds the constructor you need, and call much the same code as above internally.

public class MyAutoCompleteStringCollection : AutoCompleteStringCollection
{
  public MyAutoCompleteStringCollection(IEnumerable items)
  {
     this.AddRange(items.ToArray())
  }
}

Thus you can use

_textbox.AutoCompleteCustomSource = new MyAutoCompleteStringCollection (user.GetNameUsers());

Personally, i'd go with option 1 for now.

Spectroscopy answered 13/1, 2011 at 8:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.