TextBox联想功能

     我们在用百度的时都会发现,当输入一个词就会下拉出一系列相关的词句,这个就是今天要讲的TextBox联想(针对Winform)。

  首先我们要用的一个类是AutoCompleteStringCollection,字面意思是自动匹配完成字符串。详细可以在http://msdn.microsoft.com/zh-tw/library/windows/apps/xaml/system.windows.forms.autocompletestringcollection查看。

  现在我们看一个实例

 1 //文本输入下拉相关内容
 2         private void textSuggeust( List<string> list)
 3         {
 4             AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
 5            
 6             foreach (string str in list)
 7             {
 8                 collection.Add(str);
 9             }
10             txt_name.AutoCompleteCustomSource = collection;
11         }

  在窗体加载的时候调用此方法,再设置TextBox的AutoCompeletMode为Suggest (如果联想后自动填写到TextBox则可以设置为SuggestAppend),联想源AutoCompeletSource设置为CustomSource,

 

posted @ 2012-07-21 11:58  诸葛风流  阅读(1425)  评论(0编辑  收藏  举报