C# 自动完成TestRingCollection,每个字符串带有标记
是否可以在C# 自动完成TestRingCollection,每个字符串带有标记,c#,.net,winforms,C#,.net,Winforms,是否可以在AutoCompleteStringCollection中为字符串项添加一个标记?因为在我的研究中没有我自己的方法,我试着自己去实现它。但现在的问题是,当用户单击AutoCompleteStringCollection中的条目时,它似乎不会触发额外的事件,例如在文本框中 是否有可能以本机方式实现这一点 如果不是,我如何知道用户单击了什么索引,以便我可以判断输入是手动输入的(TextChanged-Event)还是通过选择(??-Event) 由于似乎没有其他解决方案,我已经实现了自己的
AutoCompleteStringCollection
中为字符串项添加一个标记
?因为在我的研究中没有我自己的方法,我试着自己去实现它。但现在的问题是,当用户单击AutoCompleteStringCollection
中的条目时,它似乎不会触发额外的事件,例如在文本框中
是否有可能以本机方式实现这一点
如果不是,我如何知道用户单击了什么索引,以便我可以判断输入是手动输入的(TextChanged
-Event)还是通过选择(??-Event)
由于似乎没有其他解决方案,我已经实现了自己的解决方案。
如果你想用这个。您可以使用TextBox
或ComboBox
的TextChanged
-事件,使用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);
}
}
公共类TaggedAutoCompleteStringCollection:AutoCompleteTestRingCollection
{
私有列表标签;
公共标记DautoCompleteStringCollection()
:base()
{
_标签=新列表();
}
公共int Add(字符串值、对象标记)
{
int result=this.Add(值);
_标签。添加(标签);
返回结果;
}
public void AddRange(字符串[]值,对象[]标记)
{
base.AddRange(值);
_tags.AddRange(tag);
}
公共新空白清除()
{
base.Clear();
_tags.Clear();
}
公共bool ContainsTag(对象标记)
{
返回标签。包含(标签);
}
public int IndexOfTag(对象标记)
{
返回_tags.IndexOf(tag);
}
公共void插入(int索引、字符串值、对象标记)
{
基础。插入(索引、值);
_标签。插入(索引,标签);
}
公共新空删除(字符串值)
{
int index=this.IndexOf(值);
如果(索引!=-1)
{
基本移动(索引);
_标签移除(索引);
}
}
公共新空移除(内部索引)
{
基本移动(索引);
_标签移除(索引);
}
公共对象标记字符串(字符串值)
{
int index=base.IndexOf(值);
如果(索引==-1)
{
返回null;
}
返回标签[索引];
}
}
公共静态类TaggedAutoCompleteStringCollectionHelper
{
公共静态对象GetTag(此文本框控件)
{
var source=control.AutoCompleteCustomSource作为TaggedAutoCompleteStringCollection;
if(source==null)
{
返回null;
}
返回source.TagOfString(control.Text);
}
公共静态对象GetTag(此组合框控件)
{
var source=control.DataSource作为TaggedAutoCompleteStringCollection;
if(source==null)
{
返回null;
}
返回source.TagOfString(control.Text);
}
}
您可以通过if(AutoComplStrCol.Contains(TextBox1.Text))检查它是否是集合的一个元素。
是的,我已经这样做了。但这似乎是一个糟糕的解决方案,因为它是1。慢和2。当集合中有两个相同的字符串时,这不能保证函数正确。