带有用户名数据的 AutoCompleteCustomSource 不工作
AutoCompleteCustomSource with user names data isn't working
我正在尝试创建一个具有自动完成功能的文本框。
在我的表单的构造函数中,我从数据库获取数据并将 TextBox AutoCompleteCustomSource
属性 设置为用户名数组。
由于某种原因,自动完成功能无法正常工作。
我确定db.getUsersList()
方法没有问题(底部截图)。
public mainPanel()
{
InitializeComponent();
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
collection.AddRange(db.getUserList().ToArray());
nickName.AutoCompleteCustomSource = collection;
}
要设置支持自动完成的控件,需要指定自动完成功能的来源。当使用 AutoCompleteCustomSource property, the AutoCompleteSource property must be set to AutoCompleteSource.CustomSource and AutoCompleteMode 设置为字符串集合时,设置为 AutoCompleteMode.SuggestAppend
或 AutoCompleteMode.Suggest
。
这些属性 必须一起使用 以指定自动完成功能的工作方式。
由于问题中的代码似乎正在使用某种数据源来创建 AutoCompleteCustomSource
集合,因此这是一个从 List<class>
创建 CustomSource
的通用示例,使用 Binding
class 向控件添加绑定,并使用 BindingSource
更新某些控件的值。
该示例,如视觉示例所示,使用了三个控件:一个文本框 (txtAutoComp
),其中启用了自动完成功能,以及两个标签 (lblNickName
和lblNickValue
),绑定到同一个数据源,当AutoComple控件的文本发生变化时更新。
自动完成已扩展为允许使用部分字符串查找元素,单击按钮(btnFindNick
,此处)或按文本框中的 Enter
键:
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)
);
我正在尝试创建一个具有自动完成功能的文本框。
在我的表单的构造函数中,我从数据库获取数据并将 TextBox AutoCompleteCustomSource
属性 设置为用户名数组。
由于某种原因,自动完成功能无法正常工作。
我确定db.getUsersList()
方法没有问题(底部截图)。
public mainPanel()
{
InitializeComponent();
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
collection.AddRange(db.getUserList().ToArray());
nickName.AutoCompleteCustomSource = collection;
}
要设置支持自动完成的控件,需要指定自动完成功能的来源。当使用 AutoCompleteCustomSource property, the AutoCompleteSource property must be set to AutoCompleteSource.CustomSource and AutoCompleteMode 设置为字符串集合时,设置为 AutoCompleteMode.SuggestAppend
或 AutoCompleteMode.Suggest
。
这些属性 必须一起使用 以指定自动完成功能的工作方式。
由于问题中的代码似乎正在使用某种数据源来创建 AutoCompleteCustomSource
集合,因此这是一个从 List<class>
创建 CustomSource
的通用示例,使用 Binding
class 向控件添加绑定,并使用 BindingSource
更新某些控件的值。
该示例,如视觉示例所示,使用了三个控件:一个文本框 (txtAutoComp
),其中启用了自动完成功能,以及两个标签 (lblNickName
和lblNickValue
),绑定到同一个数据源,当AutoComple控件的文本发生变化时更新。
自动完成已扩展为允许使用部分字符串查找元素,单击按钮(btnFindNick
,此处)或按文本框中的 Enter
键:
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)
);