我有一个要求,在自动填充文本框中,当我们选择将在文本框中加载的建议时再次输入时,新选择应加载到同一文本框中(带有“,”或空格)而不是空旧的。有没有可能这样做。这是我的一般自动完成我在表单加载中调用它。
if (objMaster == null) objMaster = new MasterAsariReturn();
dt = new DataTable();
dt = objMaster.GetNameData();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
nameCollection.Add(dt.Rows[i][0].ToString());
}
txtProductSpec.AutoCompleteMode = AutoCompleteMode.Suggest;
txtProductSpec.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtProductSpec.AutoCompleteCustomSource = nameCollection;
}
答案 0 :(得分:1)
我不确定这是不是你想要的,但我还是去了。我的解决方案是每次将新项目添加到列表时不断更新自动完成列表。
在表单构造函数或表单加载事件中:
public Form1()
{
InitializeComponent();
FullAutoCompleteSelection.Add("Item1");
FullAutoCompleteSelection.Add("Item2");
FullAutoCompleteSelection.Add("Item3");
FullAutoCompleteSelection.Add("Item4");
FullAutoCompleteSelection.Add("Item5");
FullAutoCompleteSelection.Add("Item6");
FullAutoCompleteSelection.Add("Item7");
FullAutoCompleteSelection.Add("Item8");
FullAutoCompleteSelection.Add("Item9");
autoCompleteTestBox.AutoCompleteMode = AutoCompleteMode.Suggest;
autoCompleteTestBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
UpdateAutoCompleteSource();
}
然后我有以下类(表单)私有成员:
private List<string> FullAutoCompleteSelection = new List<string>();
private AutoCompleteStringCollection CurrentAutoCompleteSelection = new AutoCompleteStringCollection();
我还在文本框的TextChanged事件中进行测试,看看是否需要更新自动完成列表:
private void autoCompleteTestBox_TextChanged(object sender, EventArgs e)
{
if(CurrentAutoCompleteSelection.Count == 0)
{
UpdateAutoCompleteSource();
return;
}
if (CurrentAutoCompleteSelection.Contains(autoCompleteTestBox.Text))
{
UpdateAutoCompleteSource();
return;
}
string[] textItems = autoCompleteTestBox.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string[] checkItems = CurrentAutoCompleteSelection[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (textItems.Length < checkItems.Length - 1)
{
UpdateAutoCompleteSource();
return;
}
for (int i = 0; i < checkItems.Length - 1; i++)
{
if (textItems[i].Trim() != checkItems[i].Trim())
{
UpdateAutoCompleteSource();
return;
}
}
}
最后我有一个UpdateAutoCompleteSource方法:
private void UpdateAutoCompleteSource()
{
string[] textItems = autoCompleteTestBox.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
List<string> preSelectedItems = new List<string>();
foreach (string item in textItems)
{
if (FullAutoCompleteSelection.Contains(item.Trim())) { preSelectedItems.Add(item.Trim()); }
}
List<string> autoCompleteList = FullAutoCompleteSelection.ToList();
StringBuilder preString = new StringBuilder();
foreach (string selectedItem in preSelectedItems)
{
if (autoCompleteList.Contains(selectedItem)) { autoCompleteList.Remove(selectedItem); }
preString.Append(selectedItem + ", ");
}
CurrentAutoCompleteSelection = new AutoCompleteStringCollection();
foreach (string item in autoCompleteList)
{
CurrentAutoCompleteSelection.Add(preString + item);
}
autoCompleteTestBox.AutoCompleteCustomSource = CurrentAutoCompleteSelection;
}