AutoCompleteCustomSource with user names data isn&

2020-04-30 01:44发布

I am trying to create a TextBox with auto completion.
In the constructor of my Form, I am getting data from a database and set the TextBox AutoCompleteCustomSource property to the user names array.
For some reason, the autocomplete feature is not working.

I am sure that there are no problems with the db.getUsersList() method (screenshot at the bottom).

public mainPanel()
{
    InitializeComponent();
    AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
    collection.AddRange(db.getUserList().ToArray());
    nickName.AutoCompleteCustomSource = collection;
}

enter image description here

1条回答
祖国的老花朵
2楼-- · 2020-04-30 02:40

To setup a control that supports auto completion, it's necessary to specify the source of the AutoComplete feature. When set to a string collection using the AutoCompleteCustomSource property, the AutoCompleteSource property must be set to AutoCompleteSource.CustomSource and AutoCompleteMode set to either AutoCompleteMode.SuggestAppend or AutoCompleteMode.Suggest.

These properties must be used together to specify how the AutoComplete feature works.

Since it appears the code in the question is using some sort of data source to create the AutoCompleteCustomSource collection, here's a generic example that creates a CustomSource from a List<class>, adds bindings to the controls using a Binding class and updates the values of some controls using a BindingSource.

The example, as seen it visual sample, uses three control: a TextBox (txtAutoComp), where the AutoComplete feature is enabled, and two Labels (lblNickName and lblNickValue), bound to the same data source, which are updated when the text of AutoComple control changes.
The AutoComplete is expanded to allow to find elements using partial strings, either clicking a Button (btnFindNick, here) or pressing the Enter key in the TextBox:

TextBox AutoCompleteCustomSource

private class NickName
{
    public string Nick { get; set; }
    public int Value { get; set; }
}

private BindingSource source = null;
private List<NickName> NickNames = null;

private void Form1_Load(object sender, EventArgs e)
{
    NickNames = new List<NickName>();
    NickNames.AddRange(new[] {
        new NickName() { Nick = "", Value = 0 },
        new NickName() { Nick = "Andrea", Value = 10 },
        new NickName() { Nick = "Arnold", Value = 20 },
        new NickName() { Nick = "Barbara", Value = 30 },
        new NickName() { Nick = "Billy", Value = 40 },
        new NickName() { Nick = "Clint", Value = 50 },
        new NickName() { Nick = "Cindy", Value = 60 },
    });
    source = new BindingSource();
    source.DataSource = NickNames;

    txtAutoComp.AutoCompleteMode = AutoCompleteMode.Suggest;
    txtAutoComp.AutoCompleteSource = AutoCompleteSource.CustomSource;
    txtAutoComp.AutoCompleteCustomSource.AddRange(NickNames.Select(n => n.Nick).ToArray());

    Binding textBind = new Binding("Text", source, "Nick", true, DataSourceUpdateMode.OnPropertyChanged);
    textBind.Parse += (s, evt) => {
        source.CurrencyManager.Position = NickNames.FindIndex(1, r => r.Nick.Equals(evt.Value));
    };

    txtAutoComp.DataBindings.Add(textBind);
    lblNickName.DataBindings.Add(new Binding("Text", source, "Nick"));
    lblNickValue.DataBindings.Add(new Binding("Text", source, "Value"));
}

private void btnFindNick_Click(object sender, EventArgs e)
{
    FindNick(txtAutoComp.Text);
}

private void txtAutoComp_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter) {
        e.SuppressKeyPress = true;
        FindNick(txtAutoComp.Text);
    }
}

void FindNick(string partialName) 
    => this.source.CurrencyManager.Position = NickNames.FindIndex(
        1, r => r.Nick.Contains(partialName)
    );
查看更多
登录 后发表回答