C#中的自动完成框 [英] Auto complete box in C#

查看:62
本文介绍了C#中的自动完成框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自动完成框.例如我写一个字符
上午" ,则应在文本框中建议所有数据.下面的代码在文本更改事件上触发:

I want to create a Autocomplete box. For example I write a character
''am'' then all data should be suggested in textbox. Below code fire on text change event:

string search = textBox1.Text + '%';
SqlConnection cn = new SqlConnection(objcommn.getebitobiDatabase);
SqlCommand cmd = new SqlCommand("select firstname + ' ' + lastname as fullname from user_catalog where firstname like '" + search + "' or lastname like '" + search + "' ", cn);
//sqldataadapter  dadp = new sqldataadapter(cmd);
//datatable  dt = new datatable();
//dadp.fill(dt);
cn.Open();
SqlDataReader dreader = cmd.ExecuteReader();
if (dreader.HasRows == true)
{
    while (dreader.Read())
        namesCollection.Add(dreader["fullname"].ToString());
}
else
{
    MessageBox.Show("data not found");
} dreader.Close();
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = namesCollection;


请帮帮我.
谢谢.


Please help me.
Thanks.

推荐答案

用户每次在文本框中键入内容时,都不应更新自动完成源-这会引起问题.取而代之的是,自动完成源仅应在用户开始键入之前用所有值的范围进行初始化.然后,当用户在框中输入新字符时,该文本框将减少从源中显示的建议.

换句话说,您不需要从自动完成源中手动删除与用户键入的内容不匹配的项目.您所要做的就是将所有可能的内容放入自动完成源中,文本框将在用户键入时自动建议适当的值.
The auto complete source shouldn''t be updated every time that the user types something in the text box - that will cause problems. Instead, the auto complete source should be initialized with the full range of values only once before the user starts typing. Then the text box will pare down what it suggests from the source as the user enters new characters in the box.

Another way of saying it is that you don''t need to manually remove items from the auto complete source that don''t match what the user is typing. All you have to do is put everything possible in the auto complete source and the text box will suggest the appropriate values automatically as the user types.


这篇关于C#中的自动完成框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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