IDE:Visual Studio,Winforms,C#.net 4.0
我正在创建一个具有建议功能的textBox。问题是它给出了例如初始字母的建议。假设source包含' hello user'当用户输入' user'它不会给出任何建议, 为了处理这种情况,我写了以下代码:
private void Type2_Load(object sender, EventArgs e)
{
source = new List<string>();
source.Add("aaaaaa");
source.Add("bbbbbb");
source.Add("cccccc");
source.Add("aa");
source.Add("yogesshaaa");
source.Add("yogesh aaa");
BindTextBox(source);
}
private void BindTextBox(List<string> bindWith)
{
// txt.Invalidate();
ss = new AutoCompleteStringCollection();
txt.AutoCompleteCustomSource = null;
txt.AutoCompleteMode = AutoCompleteMode.None;
txt.AutoCompleteSource = AutoCompleteSource.None;
// ss.AddRange(bindWith.ToArray());
txt.AutoCompleteCustomSource = ss;
txt.AutoCompleteMode = AutoCompleteMode.Suggest;
txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
ss.AddRange(bindWith.ToArray());
}
private void txt_TextChanged(object sender, EventArgs e)
{
List<string> lstNewList = new List<string>();
foreach (string s in source)
{
if(s.Contains(txt.Text))
{
lstNewList.Add(s);
}
}
BindTextBox(lstNewList);
}
在txt_TextChanged
事件中,我正在创建一个newList,其中包含要在txtBox建议中建议的单词,我正在重新绑定该textBox,但它没有给我更新建议。
请告诉我如何解决这种情况。
答案 0 :(得分:0)
我有自动填充的解决方案:
首先,您必须在文本框中调用ControlAdded事件:
private void tb_ControlAdded(object sender, ControlEventArgs e)
{
TextBox autoText = e.Control as TextBox;
if (autoText != null)
{
autoText.AutoCompleteMode = AutoCompleteMode.Suggest;
autoText.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
addItems(DataCollection);
autoText.AutoCompleteCustomSource = DataCollection;
}
}
然后创建&#34; addItems&#34;方法如下:
public void addItems(AutoCompleteStringCollection col)
{
col.Add("Value 1");
col.Add("Value 2");
}
我希望这能解决你的问题...祝你有愉快的一天!