Products表中有[ProductID],[ProductName],[ProductNameSpell],[MValue]四列
想在文本框中输入的时候自动显示对应的ProductName
        private void tb_productName_TextChanged(object sender, EventArgs e)
        {
            AutoCompleteStringCollection cs = new AutoCompleteStringCollection();
            DataTable dt = pm.GetProductByName(tb_productName.Text.Trim());     
            foreach(DataRow dr in dt.Rows)     
            {
                cs.Add(dr["ProductName"].ToString());
            }  
            tb_productName.AutoCompleteCustomSource = cs;
            tb_productName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            tb_productName.AutoCompleteSource = AutoCompleteSource.CustomSource;
        }
比如Products中有豆油、豆粕,我在文本框中输入d的时候显示不出来,只有输入汉字的时候才可以显示出来提示。
但是调试的时候输入d时,cs的确包含豆油和豆粕,求教啊~

解决方案 »

  1.   

    这个问题在Windows Vista以前的操作系统无解,之后的系统可以通过API自定义过滤集合实现。你的理解是错误的。AutoCompleteStringCollection包含的是所有可能被建议的项目,而究竟哪些被显示,是系统决定的,和AutoCompleteStringCollection的内容无关,系统只会找开头匹配的显示出来。你在TextChanged中修改这个集合毫无意义。
      

  2.   

    http://msdn.microsoft.com/zh-cn/library/windows/desktop/bb762479(v=vs.85).aspxACO_WORD_FILTER
    0x0080. Windows Vista and later. If set, the autocompleted suggestion is treated as a phrase for search purposes. The suggestion, Microsoft Office, would be treated as "Microsoft Office" (where both Microsoft AND Office must appear in the search results).
    ACO_NOPREFIXFILTERING
    0x0100. Windows Vista and later. Disable prefix filtering when displaying the autosuggest dropdown. Always display all suggestions.你注意,这两个特性是Vista才有的。如果你能保证你的系统是运行在这样的版本上,你可以用API SHAutoComplete IAutoComplete2::SetOptions等等。
      

  3.   

    实现起来比较麻烦,如果你有兴趣,可以看蒋版的
    http://jiangsheng.net/2007/08/06/autocomplete-with-datasource/?like=1
      

  4.   

    谢谢啦,顺便请教个问题我是新手,问得问题有点小白,不要介意哈。
    3层架构里面的实体层的类中如何体现外键,一直没有找到个demo,可耻了
      

  5.   

    本帖最后由 caozhy 于 2012-11-16 21:34:46 编辑