当前位置:  开发笔记 > 编程语言 > 正文

在Winforms中控制自动完成的标签?

如何解决《在Winforms中控制自动完成的标签?》经验,为你挑选了1个好方法。

我正在寻找一个WinForm控件,它将为多个空格分隔提供自动完成行为 - 完全是ala del.icio.us(或者就此问题而言是stackoverflow.com).

有谁知道如何在.NET 2.0 WinForm应用程序中这样做?



1> Jakub Kotrla..:

ComboBox可以自动完成,但一次只能有一个单词.

如果你想让每个单词分别自动完成,你必须自己编写.

我已经做过,希望不会太久.它不是100%完全符合您的要求,在电子邮件地址中输入时用于自动填充电子邮件客户端.

/// 
/// Extended TextBox with smart auto-completion
/// 
public class TextBoxAC: TextBox
{
    private List completions = new List();
    private List completionsLow = new List();
    private bool autocompleting = false;
    private bool acDisabled = true;

    private List possibleCompletions = new List();
    private int currentCompletion = 0;

    /// 
    /// Default constructor
    /// 
    public TextBoxAC()
    {
        this.TextChanged += new EventHandler(TextBoxAC_TextChanged);
        this.KeyPress += new KeyPressEventHandler(TextBoxAC_KeyPress);
        this.KeyDown += new KeyEventHandler(TextBoxAC_KeyDown);

        this.TabStop = true;
    }

    /// 
    /// Sets autocompletion data, list of possible strings
    /// 
    /// Completion words
    /// Completion words in lowerCase
    public void SetAutoCompletion(List words, List 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;
    }
}

推荐阅读
mylvfamily
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有