c#自动完成组合框

 

介绍 我需要创建一个

  

 

组合框控件,当用户输入时自动完成。我在Code Project中搜索了很多有用的资源,发现了Chris Maunder在MFC中的一个很好的例子。 我的需求是,我想在我的项目中使用完全托管的代码,并且,由于Chris的控件是用MFC编写的,所以我决定基于他的控件,使用我自己的。我已经向该控件添加了一个附加属性,它将该控件限制为项目列表。 控件的所有魔力都发生在OnTextChanged事件中。_inEditMode字段是在OnKeyDown事件中根据是否按下退格键或删除键设置的。如果该字段为真,则代码将查找部分字符串,选择用户已经键入的部分。 OnValidating被覆盖,在LimitToList属性为true的情况下触发NotInList事件,允许用户在输入不在项目列表中的文本时执行某些操作。 OnNotInList也作为一个受保护的虚拟方法提供,以便派生类可以调用事件处理程序而不订阅接收其事件。 源代码隐藏收缩,复制Code

using System;
using System.Windows.Forms;
using System.ComponentModel;

namespace MattBerther.Controls
{
    public class AutoCompleteComboBox : System.Windows.Forms.ComboBox
    {
        public event System.ComponentModel.CancelEventHandler NotInList;

        private bool _limitToList = true;
        private bool _inEditMode = false;

        public AutoCompleteComboBox() : base()
        {
        }

        [Category("Behavior")]
        public bool LimitToList
        {
            get { return _limitToList; }
            set { _limitToList = value; }
        }

        protected virtual void 
            OnNotInList (System.ComponentModel.CancelEventArgs e)
        {
            if (NotInList != null)
            {
                NotInList(this, e);
            }
        }   

        protected override void OnTextChanged(System.EventArgs e)
        {
            if (_inEditMode)
            {
                string input = Text;
                int index = FindString(input);

                if (index >= 0)
                {
                    _inEditMode = false;
                    SelectedIndex = index;
                    _inEditMode = true;
                    Select(input.Length, Text.Length);
                }
            }

            base.OnTextChanged(e);
        }

        protected override void 
            OnValidating(System.ComponentModel.CancelEventArgs e)
        {
            if (this.LimitToList)
            {
                int pos = this.FindStringExact(this.Text);
        
                if (pos == -1)
                {
                    OnNotInList(e);
                }
                else
                {
                    this.SelectedIndex = pos;
                }
            }

            base.OnValidating(e);
        }

        protected override void 
            OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            _inEditMode = 
                (e.KeyCode != Keys.Back && e.KeyCode != Keys.Delete);
            base.OnKeyDown(e);
        }
    }
}

使用这种控制 使用此控件的方式与使用常规System.Windows.Forms.ComboBox的方式相同。通过设计器或下面详细介绍的编程方式将其添加到窗体中。隐藏,复制Code

protected override void OnLoad(System.EventArgs e)
{
    base.OnLoad(e);

    MattBerther.Controls.AutoCompleteComboBox cb = new 
        MattBerther.Controls.AutoCompleteComboBox();

    cb.Items.Add("Apples");
    cb.Items.Add("Oranges");
    cb.Items.Add("Grapefruits");

    cb.LimitToList = true;
    cb.NotInList += new CancelEventHandler(combobox_NotInList);
    this.Controls.Add(cb);
}

private void combobox_NotInList(object sender, CancelEventArgs e)
{
    MessageBox.Show("You entered a value that was not 
        consistent with the list provided. Please try again.");
    e.Cancel = true;
}

再次感谢Chris Maunder,他为我在完全托管代码中实现自己的自动完成组合框提供了一个极好的起点。 历史 1.0 - 04.15.2003 -首次发布版本 本文转载于:http://www.diyabc.com/frontweb/news227.html

posted @ 2020-08-04 08:01  Dincat  阅读(291)  评论(0编辑  收藏  举报