这样方便很多用户更简单使用你设计的系统.在winform设计中,你只需要使用他们的AutoCompleteMode,AutoCompleteSource二个
方法.下面是示例:
首先建立一个项目,在Form1上拉入一个TextBox,与ComboBox控件.进去代码界面,在Form1_load事件中写进下面代码:
private
void
Form1_Load(
object
sender, EventArgs e)
{
textBox1.AutoCompleteCustomSource.AddRange( new string [] { " James " , " KoBe " , " T-MC " , " JamesLee " , " King " , " ToT " });
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
comboBox1.Items.AddRange( new string [] { " James " , " KoBe " , " T-MC " , " JamesLi " , " King " , " TT " });
comboBox1.AutoCompleteCustomSource.AddRange( new string [] { " James " , " KoBe " , " T-MC " , " JamesLee " , " King " , " ToT " });
comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
{
textBox1.AutoCompleteCustomSource.AddRange( new string [] { " James " , " KoBe " , " T-MC " , " JamesLee " , " King " , " ToT " });
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
comboBox1.Items.AddRange( new string [] { " James " , " KoBe " , " T-MC " , " JamesLi " , " King " , " TT " });
comboBox1.AutoCompleteCustomSource.AddRange( new string [] { " James " , " KoBe " , " T-MC " , " JamesLee " , " King " , " ToT " });
comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
注意:AutoCompleteMode有Append,None,Suggest,SuggestAppend四种属性:
Append就是把第一个相似的项追加到你输入字符的后面
None就是不做任何提示
Suggest就是把相似的项用列表的方法显示在下面
SuggestAppend就是把第一个相似的项加到你输入字符的后面并在下面用列表显示所有相似的项
AutoCompleteCustomSource属性的使用是可选的,但必须将 AutoCompleteSource 属性设置为CustomSource后才能使用AutoCompleteCustomSource.
AutoCompleteMode 和 AutoCompleteSource 属性必须一起使用。
上面的代码运行的结果如下图: