问题描述
我正在尝试创建一个具有自动完成功能的文本框.
在表单的构造函数中,我从数据库中获取数据并将 TextBox AutoCompleteCustomSource
属性设置为用户名数组.
由于某种原因,自动完成功能无法正常工作.
我确信 db.getUsersList()
方法没有问题(截图在底部).
public mainPanel(){初始化组件();AutoCompleteStringCollection 集合 = 新 AutoCompleteStringCollection();集合.AddRange(db.getUserList().ToArray());nickName.AutoCompleteCustomSource = 集合;}
要设置支持自动完成的控件,需要指定自动完成功能的来源.当使用
私有类 NickName{公共字符串尼克 { 得到;放;}公共 int 值 { 获取;放;}}私有 BindingSource 源 = null;私人名单<昵称>昵称 = null;私人无效Form1_Load(对象发送者,EventArgs e){昵称 = 新列表<昵称>();NickNames.AddRange(new[] {新的 NickName() { Nick = "", Value = 0 },新的 NickName() { Nick = "Andrea", Value = 10 },新的 NickName() { Nick = "Arnold", Value = 20 },新的 NickName() { Nick = "Barbara", Value = 30 },新的 NickName() { Nick = "Billy", Value = 40 },新的 NickName() { Nick = "Clint", Value = 50 },新的 NickName() { Nick = "Cindy", Value = 60 },});源 = 新的 BindingSource();source.DataSource = 昵称;txtAutoComp.AutoCompleteMode = AutoCompleteMode.Suggest;txtAutoComp.AutoCompleteSource = AutoCompleteSource.CustomSource;txtAutoComp.AutoCompleteCustomSource.AddRange(NickNames.Select(n => n.Nick).ToArray());绑定 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"));}私人无效btnFindNick_Click(对象发送者,EventArgs e){FindNick(txtAutoComp.Text);}私人无效txtAutoComp_KeyDown(对象发送者,KeyEventArgs e){if (e.KeyCode == Keys.Enter) {e.SuppressKeyPress = true;FindNick(txtAutoComp.Text);}}无效 FindNick(字符串部分名称)=>this.source.CurrencyManager.Position = NickNames.FindIndex(1, r =>r.Nick.Contains(partialName));
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;
}
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:
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 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!