C# ComboBox文本框模糊绑定

版权声明:个人速查笔记 https://blog.csdn.net/qq_33981438/article/details/82833402

各种原因吧 改变ComboBox的值会导致各种不舒服 比如delete 使用起来很不友好
然后我选择了新增了一个文本框
在这里插入图片描述
大概就是做这么一个东西 模糊查询文本框的值

		/// <summary>
        /// 初始化数据库名字下拉框
        /// </summary>
        public void InitCbDate(ref List<string> list)
        {
            list = S_Data.QueryDataNames().OrderBy(e=>e).Reverse().ToList();
            ComboBox data = this.cbData;
            data.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            data.DataSource = list;
        }
		//常驻的list 我们不能每次都去重复查询 
        private static List<string> dataList;
		//文本的更改并触发控件的绑定
		private void txtKey_TextChanged(object sender, EventArgs e)
        {
            this.cbData.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
            this.cbData.Text = this.txtKey.Text;
            var text = this.cbData.Text;
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.IBeam;
            Cursor = System.Windows.Forms.Cursors.Default;
            if (!string.IsNullOrEmpty(text.Trim()))
            {
                List<string> list = dataList.Where(v => v.Contains(text)).ToList();
                if (list.Count > 0)
                 {
                    cbData.DataSource = list;
                    this.cbData.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                    this.cbData.DroppedDown = true;
                 }
                else
                 {
                    cbData.DataSource = dataList;
                    this.cbData.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                    this.cbData.DroppedDown = true;
                }
            }
            else
            {
                 cbData.DataSource = dataList;
                 this.cbData.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                 this.cbData.DroppedDown = true;
            }
        }

猜你喜欢

转载自blog.csdn.net/qq_33981438/article/details/82833402