Winforms中自动完成标记的控件?
我正在寻找一个WinForm控件,该控件将为多个分隔空间提供自动完成行为—确切地说是ala del.icio.us(或stackoverflow.com)Winforms中自动完成标记的控件?,winforms,controls,autocomplete,tags,Winforms,Controls,Autocomplete,Tags,我正在寻找一个WinForm控件,该控件将为多个分隔空间提供自动完成行为—确切地说是ala del.icio.us(或stackoverflow.com) 有人知道如何在.NET 2.0 WinForm应用程序中实现这一点吗?ComboBox可以自动完成,但一次只能完成一个单词 如果你想让每个单词单独自动完成,你必须自己写 我已经看过了,希望不会太长。这不是你想要的100%,这是用来在电子邮件客户端自动完成时,键入电子邮件地址 /// <summary> /// Extended T
有人知道如何在.NET 2.0 WinForm应用程序中实现这一点吗?ComboBox可以自动完成,但一次只能完成一个单词 如果你想让每个单词单独自动完成,你必须自己写 我已经看过了,希望不会太长。这不是你想要的100%,这是用来在电子邮件客户端自动完成时,键入电子邮件地址
/// <summary>
/// Extended TextBox with smart auto-completion
/// </summary>
public class TextBoxAC: TextBox
{
private List<string> completions = new List<string>();
private List<string> completionsLow = new List<string>();
private bool autocompleting = false;
private bool acDisabled = true;
private List<string> possibleCompletions = new List<string>();
private int currentCompletion = 0;
/// <summary>
/// Default constructor
/// </summary>
public TextBoxAC()
{
this.TextChanged += new EventHandler(TextBoxAC_TextChanged);
this.KeyPress += new KeyPressEventHandler(TextBoxAC_KeyPress);
this.KeyDown += new KeyEventHandler(TextBoxAC_KeyDown);
this.TabStop = true;
}
/// <summary>
/// Sets autocompletion data, list of possible strings
/// </summary>
/// <param name="words">Completion words</param>
/// <param name="wordsLow">Completion words in lowerCase</param>
public void SetAutoCompletion(List<string> words, List<string> wordsLow)
{
if (words == null || words.Count < 1) { return; }
this.completions = words;
this.completionsLow = wordsLow;
this.TabStop = false;
}
private void TextBoxAC_TextChanged(object sender, EventArgs e)
{
if (this.autocompleting || this.acDisabled) { return; }
string text = this.Text;
if (text.Length != this.SelectionStart) { return; }
int pos = this.SelectionStart;
string userPrefix = text.Substring(0, pos);
int commaPos = userPrefix.LastIndexOf(",");
if (commaPos == -1)
{
userPrefix = userPrefix.ToLower();
this.possibleCompletions.Clear();
int n = 0;
foreach (string s in this.completionsLow)
{
if (s.StartsWith(userPrefix))
{
this.possibleCompletions.Add(this.completions[n]);
}
n++;
}
if (this.possibleCompletions.Count < 1) { return; }
this.autocompleting = true;
this.Text = this.possibleCompletions[0];
this.autocompleting = false;
this.SelectionStart = pos;
this.SelectionLength = this.Text.Length - pos;
}
else
{
string curUs = userPrefix.Substring(commaPos + 1);
if (curUs.Trim().Length < 1) { return; }
string trimmed;
curUs = this.trimOut(curUs, out trimmed);
curUs = curUs.ToLower();
string oldUs = userPrefix.Substring(0, commaPos + 1);
this.possibleCompletions.Clear();
int n = 0;
foreach (string s in this.completionsLow)
{
if (s.StartsWith(curUs))
{
this.possibleCompletions.Add(this.completions[n]);
}
n++;
}
if (this.possibleCompletions.Count < 1) { return; }
this.autocompleting = true;
this.Text = oldUs + trimmed + this.possibleCompletions[0];
this.autocompleting = false;
this.SelectionStart = pos;
this.SelectionLength = this.Text.Length - pos + trimmed.Length;
}
this.currentCompletion = 0;
}
private void TextBoxAC_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
{
this.acDisabled = true;
}
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
{
if ((this.acDisabled) || (this.possibleCompletions.Count < 1))
{
return;
}
e.Handled = true;
if (this.possibleCompletions.Count < 2) { return; }
switch (e.KeyCode)
{
case Keys.Up:
this.currentCompletion--;
if (this.currentCompletion < 0)
{
this.currentCompletion = this.possibleCompletions.Count - 1;
}
break;
case Keys.Down:
this.currentCompletion++;
if (this.currentCompletion >= this.possibleCompletions.Count)
{
this.currentCompletion = 0;
}
break;
}
int pos = this.SelectionStart;
string userPrefix = this.Text.Substring(0, pos);
int commaPos = userPrefix.LastIndexOf(",");
if (commaPos == -1)
{
pos--;
userPrefix = this.Text.Substring(0, pos);
this.autocompleting = true;
this.Text = userPrefix + this.possibleCompletions[this.currentCompletion].Substring(userPrefix.Length);
this.autocompleting = false;
this.SelectionStart = pos + 1;
this.SelectionLength = this.Text.Length - pos;
}
else
{
string curUs = userPrefix.Substring(commaPos + 1);
if (curUs.Trim().Length < 1) { return; }
string trimmed;
curUs = this.trimOut(curUs, out trimmed);
curUs = curUs.ToLower();
string oldUs = userPrefix.Substring(0, commaPos + 1);
this.autocompleting = true;
this.Text = oldUs + trimmed + this.possibleCompletions[this.currentCompletion];
this.autocompleting = false;
this.SelectionStart = pos;
this.SelectionLength = this.Text.Length - pos + trimmed.Length;
}
}
}
private void TextBoxAC_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsControl(e.KeyChar)) { this.acDisabled = false; }
}
private string trimOut(string toTrim, out string trim)
{
string ret = toTrim.TrimStart();
int pos = toTrim.IndexOf(ret);
trim = toTrim.Substring(0, pos);
return ret;
}
}
//
///具有智能自动完成功能的扩展文本框
///
公共类TextBoxAC:TextBox
{
私有列表完成=新列表();
private List completionsLow=新列表();
私有布尔自动完成=假;
private bool acDisabled=true;
私有列表可能完成=新列表();
私有int currentCompletion=0;
///
///默认构造函数
///
公共TextBoxAC()
{
this.TextChanged+=新事件处理程序(TextBoxAC_TextChanged);
this.KeyPress+=新的KeyPressEventHandler(TextBoxAC_KeyPress);
this.KeyDown+=新的KeyEventHandler(TextBoxAC_KeyDown);
this.TabStop=true;
}
///
///设置自动完成数据,可能的字符串列表
///
///补全词
///小写完成字
public void SetAutoCompletion(列表字、列表字慢)
{
if(words==null | | words.Count<1){return;}
这个。完成=单词;
this.completionsLow=wordsLow;
this.TabStop=false;
}
私有void TextBoxAC_TextChanged(对象发送方,事件参数e)
{
如果(this.autocompleting | | this.acDisabled){return;}
字符串文本=this.text;
如果(text.Length!=this.SelectionStart){return;}
int pos=this.SelectionStart;
字符串userPrefix=text.Substring(0,pos);
int commaPos=userPrefix.LastIndexOf(“,”);
如果(逗号==-1)
{
userPrefix=userPrefix.ToLower();
此.possibleComplements.Clear();
int n=0;
foreach(此.completionsLow中的字符串s)
{
如果(s.StartsWith(userPrefix))
{
this.possibleComplements.Add(this.completions[n]);
}
n++;
}
如果(this.possibleCompletions.Count<1){return;}
this.autocompleting=true;
this.Text=this.possibleComplements[0];
this.autocompleting=false;
this.SelectionStart=pos;
this.SelectionLength=this.Text.Length-pos;
}
其他的
{
字符串curUs=userPrefix.Substring(逗号+1);
if(curUs.Trim().Length<1){return;}
线修剪;
curUs=this.trimOut(curUs,out-trimmed);
curUs=curUs.ToLower();
字符串oldUs=userPrefix.Substring(0,逗号+1);
此.possibleComplements.Clear();
int n=0;
foreach(此.completionsLow中的字符串s)
{
如果(s.StartsWith(curUs))
{
this.possibleComplements.Add(this.completions[n]);
}
n++;
}
如果(this.possibleCompletions.Count<1){return;}
this.autocompleting=true;
this.Text=oldUs+trimmed+this.possibleComplements[0];
this.autocompleting=false;
this.SelectionStart=pos;
this.SelectionLength=this.Text.Length-pos+trimmed.Length;
}
此.currentCompletion=0;
}
私有void TextBoxAC_KeyDown(对象发送方,KeyEventArgs e)
{
if(e.KeyCode==Keys.Back | | e.KeyCode==Keys.Delete)
{
this.acDisabled=true;
}
if(e.KeyCode==Keys.Up | | e.KeyCode==Keys.Down)
{
if((this.acDisabled)| |(this.possibleComplements.Count<1))
{
返回;
}
e、 已处理=正确;
如果(this.possibleCompletions.Count<2){return;}
开关(如钥匙代码)
{
案例密钥。向上:
这是当前完成--;
如果(此.currentCompletion<0)
{
this.currentCompletion=this.possibleComplections.Count-1;
}
打破
案例键。向下:
这个.currentCompletion++;
如果(this.currentCompletion>=this.possibleComplections.Count)
{
此.currentCompletion=0;
}
打破
}
int pos=this.SelectionStart;
字符串userPrefix=this.Text.Substring(0,pos);
int commaPos=userPrefix.LastIndexOf(“,”);
如果(逗号==-1)
{
pos--;
userPrefix=this.Text.Substring(0,pos);
this.autocompleting=true;
this.Text=userPrefix+this.possibleCompletions[this.currentCompletion].Substring(userPrefix.Length);
this.autocompleting=false;
this.SelectionStart=pos+1;
this.SelectionLength=this.Text.Length-pos;
}
其他的
{
字符串curUs=userPrefix.Substring(逗号+1);
if(curUs.Trim().Length<1){return;}
线修剪;
curUs=this.trimOut(curUs,out-trimmed);
curUs=curUs.ToLower();
字符串oldUs=userPrefix.Substring(0,逗号+1);
this.autocompleting=true;
this.Text=oldUs+trimmed+this.possibleCompletions[this.currentCompleti