2008-09-25 6 views
4

여러 개의 공백으로 구분 된 - 정확하게 ala del.icio.us (또는 그 문제에 대해 stackoverflow.com)에 대한 자동 완성 동작을 제공하는 WinForm 컨트롤을 찾고 있습니다.Winforms에서 자동 완성 기능이있는 태그를 제어 하시겠습니까?

누구나 .NET 2.0 WinForm 응용 프로그램에서이를 수행하는 방법을 알고 있습니까?

+0

나는 비슷한 것을 찾고있다. 어떤 솔루션을 사용 했습니까? –

+0

없음, 아직 해결책을 찾고 있습니다. 죄송합니다. –

+0

나는 또한 비슷한 것을 찾고 있습니다. 아래 해결책을 찾았거나 아래 해결책을 사용 했습니까? – studiothat

답변

7

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; 
    } 
}