另一个具有自动完成功能的ComboBox

 根据你输入的内容自动过滤出匹配的列表项,并实时显示出来,根据网上的多个文章试验成功,现将代码贡献出来。

原理:设计一个自定义组件,组件继承之combobox,并且拥有一个listbox成员,当组件的text改变时,就从combobox中过滤出匹配的列表项,并且把这些列表项加入到listbox中显示出来。在.net 2.0 vs2005 c#下调试成功。

 


using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Collections;
using System.Windows.Forms;
namespace ComBoxtest
{
    public partial class AutoComboBox2 : System.Windows.Forms.ComboBox
    {
        private System.Windows.Forms.ListBox lbMain;
        public AutoComboBox2()
        {
            InitializeComponent();

        }

        public AutoComboBox2(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            lbMain = new ListBox();
            lbMain.Click += new EventHandler(lbMain_Click);
            lbMain.KeyDown += new KeyEventHandler(lbMain_KeyDown);
            lbMain.Visible = false;
            this.Name = "AutoComboBox2";
            this.Sorted = true;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(AutoComboBox2_KeyDown);
            this.DropDown += new System.EventHandler(AutoComboBox2_DropDown);
            this.TextChanged += new System.EventHandler(AutoComboBox2_TextChanged);
            this.SelectedIndexChanged += new System.EventHandler(AutoComboBox2_SelectedIndexChanged);
            this.Leave += new System.EventHandler(AutoComboBox2_Leave);
        }
        private ArrayList GetFillList(string strValue)
        {
            ArrayList alReturn = new ArrayList();
            int iCount = this.Items.Count;
            int iLen = strValue.Length;
            for (int i = 0; i < iCount; i++)
            {
                string strSelItem = this.Items[i].ToString();
                if (strSelItem.Length < iLen)
                    continue;
                if (strSelItem.Substring(0, iLen) == strValue)
                    alReturn.Add(strSelItem);
            }
            return alReturn;

        }
      
     
        private void lbMain_Click(object sender, System.EventArgs e)
        {
            if (lbMain.SelectedItems.Count == 0)
                return;
            string strSel = lbMain.SelectedItem.ToString();
            this.Text = strSel;
            int iSel = this.FindStringExact(Text);
            if (iSel != -1)
                this.SelectedIndex = iSel;
            lbMain.Visible = false;
        }
        private void AutoComboBox2_DropDown(object sender, System.EventArgs e)
        {
            lbMain.Visible = false;
        }
        private void AutoComboBox2_Leave(object sender, System.EventArgs e)
        {
            if (!lbMain.Focused)
                lbMain.Visible = false;
        }
        private void AutoComboBox2_TextChanged(object sender, System.EventArgs e)
        {
            if (Text == "")
            {
                lbMain.Visible = false;
                return;
            }
            if (!this.Parent.Controls.Contains(lbMain))
            {
                lbMain.Width = this.Width;
                lbMain.Height = 100;
                lbMain.Parent = this.Parent;
                lbMain.Top = this.Top + this.Height + 1;
                lbMain.Left = this.Left;
                this.Parent.Controls.Add(lbMain);
                lbMain.BringToFront();
            }
            ArrayList alFill = GetFillList(Text);
            lbMain.Items.Clear();
            lbMain.Items.AddRange(alFill.ToArray());
            if (lbMain.Items.Count > 0)
                lbMain.SelectedIndex = 0;
            if (!lbMain.Visible)
                lbMain.Visible = true;
        }
        private void lbMain_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left || e.KeyCode == Keys.PageUp)
            {
                if (lbMain.SelectedIndex > 0)
                    lbMain.SelectedIndex = lbMain.SelectedIndex - 1;
            }
            else if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right || e.KeyCode == Keys.PageDown)
            {
                if (lbMain.SelectedIndex < lbMain.Items.Count - 1)
                    lbMain.SelectedIndex = lbMain.SelectedIndex + 1;
            }
        }
        private void AutoComboBox2_SelectedIndexChanged(object sender, System.EventArgs e)
         {
           lbMain.Visible = false ;
         }
        private void AutoComboBox2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (lbMain.Visible)
            {
                if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.PageDown || e.KeyCode == Keys.PageUp)
                {
                    lbMain_KeyDown(lbMain, e);
                    e.Handled = true;
                }
                else if (e.KeyCode == Keys.Enter)
                {
                    lbMain_Click(lbMain, e);
                    e.Handled = true;
                }
            }
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            // TODO: 在此添加自定义绘画代码
            // 调用基类 OnPaint
            base.OnPaint(pe);
        }


    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值