AutoCompleteCustomSource with user names data isn#39;t working(带有用户名数据的 AutoCompleteCustomSource 不起作用)
问题描述
我正在尝试创建一个具有自动完成功能的文本框.
在表单的构造函数中,我从数据库中获取数据并将 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 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有用户名数据的 AutoCompleteCustomSource 不起作用
基础教程推荐
- 从 C# 控制相机设备 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 如果条件可以为空 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 将数据集转换为列表 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
