TextBox自动完成控件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Data;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace LHXETDDataManagement.Controls
{
    /// <summary>
    /// TextBox扩展类
    /// </summary>
    public class AutoCompleteTextBox : System.Windows.Forms.TextBox
    {
        private bool _isSelect = false;
        private ListBox _lboxSearch = null;

        public AutoCompleteTextBox()
        {
            this.DataSouce = new List<string>();
            this.PanelMaxHeight = 130; //10行
        }

        /// <summary>
        /// 获取或设置自动完成数据源
        /// </summary>
        public List<string> DataSouce { set; get; }

        /// <summary>
        /// 获取或设置面版最大高度
        /// </summary>
        public int PanelMaxHeight { set; get; }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            _isSelect = false;

            base.OnKeyPress(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (_lboxSearch == null) { return; }
            int index = _lboxSearch.SelectedIndex;
            switch (e.KeyCode)
            {
                case Keys.Up:
                    index--;
                    break;
                case Keys.Down:
                    index++;
                    break;
                case Keys.Enter:
                    LboxSearch_Click(_lboxSearch, new EventArgs());
                    break;
            }
            if (index < 0) { index = 0; }
            if (index >= _lboxSearch.Items.Count) { index = _lboxSearch.Items.Count - 1; }
            _lboxSearch.SelectedIndex = index;

            base.OnKeyDown(e);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);

            Control parent = this.Parent;
            string text = this.Text.Trim();
            if (_lboxSearch != null)
            {
                parent.Controls.Remove(_lboxSearch);
            }

            if (string.IsNullOrEmpty(text) ||
                DataSouce == null ||
                DataSouce.Count == 0)
            {
                return;
            }

            List<string> list = DataSouce.Where(p => p.Contains(text)).ToList();
            if (list.Count > 0 && !_isSelect)
            {
                int height = list.Count * 13;
                if (height >= PanelMaxHeight)
                {
                    height = PanelMaxHeight;
                }

                _lboxSearch = new ListBox();
                _lboxSearch.Location = new Point(this.Location.X, this.Location.Y + this.Size.Height);
                _lboxSearch.Size = new Size(this.Size.Width, height);
                _lboxSearch.Items.AddRange(list.ToArray());
                _lboxSearch.Click += new EventHandler(LboxSearch_Click);
                parent.Controls.Add(_lboxSearch);
                //置顶
                _lboxSearch.BringToFront();
            }
        }

        private void LboxSearch_Click(object sender, EventArgs e)
        {
            if (_lboxSearch != null)
            {
                _isSelect = true;

                this.Text = _lboxSearch.Text;
                this.Focus();
                this.SelectionStart = this.Text.Length;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值