C#ComboBox自动搜索框功能的实现(一)

本文介绍了一个使用 C# 编写的 ComboBox 控件示例,展示了如何动态填充 ComboBox 的项目,并实现文本变化时的筛选功能。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private List<string> listNames = new List<string>();
        public Form1()
        {
            InitializeComponent();
            this.comboBox1.Items.Add("ganquanfu");
            this.comboBox1.Items.Add("quanfu");
            this.comboBox1.Items.Add("ligang");
            this.comboBox1.Items.Add("zhaojialong");
            this.comboBox1.Items.Add("zhaozhen");
            this.comboBox1.Items.Add("ganquanfu");
            this.comboBox1.Items.Add("quanfu");
            this.comboBox1.Items.Add("ligang");
            this.comboBox1.Items.Add("zhaojialong");
            this.comboBox1.Items.Add("zhaozhen");
            this.comboBox1.Items.Add("ganquanfu");
            this.comboBox1.Items.Add("quanfu");
            this.comboBox1.Items.Add("ligang");
            this.comboBox1.Items.Add("zhaojialong");
            this.comboBox1.Items.Add("zhaozhen");
 
 
            foreach (var item in comboBox1.Items)
            {
                listNames.Add(item.ToString());
            }
 
            //设置DropDown的高度
           // this.comboBox1.DropDownHeight = 100;
            //以下是事件处理函数的另一种写法,Lambda表达式书写
            this.comboBox1.TextChanged += new EventHandler(comboBox1_TextChanged);
            //this.comboBox1.DropDown += (a, b) => {
            //    if (a is ComboBox)
            //    {
            //        ComboBox sender = a as ComboBox;
            //        MessageBox.Show(sender.Items[0].ToString());
            //    }
            
            
            
            //};
                this.comboBox1.DropDownClosed += new EventHandler(comboBox1_DropDownClosed);
      
       
 
           // this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
           // this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
        }
 
        void comboBox1_TextChanged(object sender, EventArgs e)
        {
            string search = comboBox1.Text.Trim();
            if (string.IsNullOrWhiteSpace(search))
            {
                return;
            }
            comboBox1.Items.Clear();
            var results = listNames.Where(t => t.Contains(search)).ToArray();
            foreach (var item in results)
            {
                comboBox1.Items.Add(item);
 
            }
            comboBox1.DroppedDown = true;
            this.Cursor = Cursors.Arrow;
            comboBox1.SelectionStart = search.Length;         }         void comboBox1_DropDownClosed(object sender, EventArgs e)         {            // MessageBox.Show(this.comboBox1.DroppedDown.ToString());         }         void comboBox1_DropDown(object sender, EventArgs e)         {            // MessageBox.Show(this.comboBox1.DroppedDown.ToString());                      }         private void button1_Click(object sender, EventArgs e)         {             //MessageBox.Show(this.comboBox1.DroppedDown.ToString());             this.comboBox1.DroppedDown = true;         }         private void Form1_Load(object sender, EventArgs e)         {             this.Cursor = System.Windows.Forms.Cursors.Cross;         }     } }

转载于:https://www.cnblogs.com/ganquanfu2008/archive/2013/05/04/3058589.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值