ComboBox输入数据时检索

来源:互联网 发布:网页游戏平台源码下载 编辑:程序博客网 时间:2024/05/22 12:38
 
 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);        }


 

原创粉丝点击