由於似乎沒有其他解決方案,我已經實現了我自己的解決方案。 如果需要,請使用此功能。您可以使用TextChanged
-TextBox
或ComboBox
的事件來使用GetTag
擴展方法獲取值。
public class TaggedAutoCompleteStringCollection : AutoCompleteStringCollection
{
private List<object> _tags;
public TaggedAutoCompleteStringCollection()
: base()
{
_tags = new List<object>();
}
public int Add(string value, object tag)
{
int result = this.Add(value);
_tags.Add(tag);
return result;
}
public void AddRange(string[] value, object[] tag)
{
base.AddRange(value);
_tags.AddRange(tag);
}
public new void Clear()
{
base.Clear();
_tags.Clear();
}
public bool ContainsTag(object tag)
{
return _tags.Contains(tag);
}
public int IndexOfTag(object tag)
{
return _tags.IndexOf(tag);
}
public void Insert(int index, string value, object tag)
{
base.Insert(index, value);
_tags.Insert(index, tag);
}
public new void Remove(string value)
{
int index = this.IndexOf(value);
if (index != -1)
{
base.RemoveAt(index);
_tags.RemoveAt(index);
}
}
public new void RemoveAt(int index)
{
base.RemoveAt(index);
_tags.RemoveAt(index);
}
public object TagOfString(string value)
{
int index = base.IndexOf(value);
if (index == -1)
{
return null;
}
return _tags[index];
}
}
public static class TaggedAutoCompleteStringCollectionHelper
{
public static object GetTag(this TextBox control)
{
var source = control.AutoCompleteCustomSource as TaggedAutoCompleteStringCollection;
if (source == null)
{
return null;
}
return source.TagOfString(control.Text);
}
public static object GetTag(this ComboBox control)
{
var source = control.DataSource as TaggedAutoCompleteStringCollection;
if (source == null)
{
return null;
}
return source.TagOfString(control.Text);
}
}
您可以通過檢查其集合的元素'如果(AutoComplStrCol.Contains(TextBox1.Text))' – SysDragon
是啊,我已經這樣做了。但它似乎是一個不好的解決方案,因爲它的1.慢和2.這不能保證正確的功能,當收集中有兩個相同的字符串。 –