ComboBox输入数据时检索

 
 public class ComboBoxCtrl
    {
        private ComboBox _ComboBox;
        private Dictionary<string, string> _Items;

        public ComboBoxCtrl(ComboBox comboBox)
        {
            _ComboBox = comboBox;

            comboBox.MouseEnter += HandleMouseEnter;
            comboBox.KeyUp += HandleKeyUp;

            _Items = new Dictionary<string, string>();
        }

        public void SetItems(Dictionary<string, string> items)
        {
            _Items = items;
        }

        public void AddItem(string key, string value)
        {
            _Items.Add(key, value);
            _ComboBox.Items.Add(key);
        }

        public void ClearItems()
        {
            _Items.Clear();
            _ComboBox.Items.Clear();
        }

        public string GetValue(string key)
        {
            if (_Items.ContainsKey(key))
            {
                return _Items[key];
            }
            else
            {
                return "Null";
            }

        }

        public string GetKey(string value)
        {
            foreach (var item in _Items)
            {
                if (item.Value == value) return item.Key;
            }

            return "Null";
        }

        private void ReloadData()
        {
            _ComboBox.Items.Clear();

            foreach (var item in _Items.Keys)
            {
                _ComboBox.Items.Add(item);
            }
        }

        public void HandleMouseEnter(object sender, EventArgs e)
        {
            ReloadData();
        }

        public void HandleKeyUp(object sender, KeyEventArgs e)
        {
            string text = _ComboBox.Text;

            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up)
            {
                return;
            }

            if (e.KeyCode == Keys.Enter)
            {
                _ComboBox.DroppedDown = false;
                return;
            }

            foreach (string item in _Items.Keys)
            {
                if (text.Length <= item.Length)
                {
                    if (item.Substring(0, text.Length) == text)
                    {
                        if (!_ComboBox.Items.Contains(item))
                        {
                            _ComboBox.Items.Add(item);
                        }
                    }
                    else
                    {
                        _ComboBox.Items.Remove(item);
                    }
                }
                else
                {
                    /*if (text.Substring(0, item.Length) == item)
                    {
                        if (!_ComboBox.Items.Contains(item))
                        {
                            _ComboBox.Items.Add(item);
                        }
                    }
                    else
                    {
                        _ComboBox.Items.Remove(item);
                    }*/
                    _ComboBox.Items.Remove(item);
                }
            }

            _ComboBox.DroppedDown = true;
        }
    }


 

 

测试上面的代码

private ComboBoxCtrl _ComboBoxCtrl;
        Dictionary<string, string> _Items = new Dictionary<string, string>();
        public Form1()
        {
            InitializeComponent();
            _ComboBoxCtrl = new ComboBoxCtrl(this.comboBox1);

            InitComboBoxData();
        }

       public void  InitComboBoxData()
        {
            _Items.Add("aaa","xyt");
            _Items.Add("aab", "as");
            _Items.Add("bbc", "sd");
            _Items.Add("bcd", "df");
            _Items.Add("ccd", "cv");
            _ComboBoxCtrl.SetItems(_Items);
        }


 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值