具有用户名数据的AutoCompleteCustomSource无法正常工作 [英] AutoCompleteCustomSource with user names data isn't working

查看:182
本文介绍了具有用户名数据的AutoCompleteCustomSource无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建具有自动完成功能的TextBox.
在我的Form的构造函数中,我正在从数据库中获取数据,并将TextBox AutoCompleteCustomSource属性设置为用户名数组.
由于某些原因,自动完成功能不起作用.

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.

我确信db.getUsersList()方法没有问题(底部截图).

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;
}

推荐答案

要设置支持自动完成的控件,必须指定自动完成"功能的来源.使用 AutoCompleteCustomSource 属性, AutoCompleteSource 属性必须设置为 AutoCompleteSource.CustomSource AutoCompleteMode 设置为AutoCompleteMode.SuggestAppendAutoCompleteMode.Suggest.

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.

因为问题中的代码似乎正在使用某种数据源来创建AutoCompleteCustomSource集合,所以下面是一个通用示例,该示例从List<class>创建CustomSource,并使用Binding类并使用BindingSource更新某些控件的值.

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.

该示例(如其可视示例所示)使用三个控件:一个文本框( txtAutoComp ),其中启用了自动完成功能,以及两个标签( lblNickName lblNickValue ),绑定到相同的数据源,当AutoComple控件的文本更改时,它们会更新.
自动完成功能已扩展为允许使用部分字符串查找元素,方法是单击按钮(在此处为 btnFindNick ,或在文本框中按Enter键):

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:

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)
    );

这篇关于具有用户名数据的AutoCompleteCustomSource无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆