C#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>();
        private int dropDownHeight = 0;
        private string txt = "";
        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());
 
            }
            dropDownHeight = comboBox1.ItemHeight * (listNames.Count + 1);
            comboBox1.DropDownHeight = dropDownHeight;
            comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
 
            //      comboBox1.Refresh(); 
            // comboBox1.SelectedIndexChanged+=new EventHandler(comboBox1_SelectedIndexChanged);
 
            //设置DropDown的高度
            // this.comboBox1.DropDownHeight = 100;
            // 以下是事件处理函数的另一种写法,Lambda表达式书写
            //   this.comboBox1.DropDown += new EventHandler(comboBox1_DropDown);
            this.comboBox1.TextUpdate += new EventHandler(comboBox1_TextUpdate);
 
            //   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.AutoCompleteMode = AutoCompleteMode.Suggest;
 
 
            //    this.comboBox1.DropDownClosed += new EventHandler(comboBox1_DropDownClosed);
            //     this.comboBox1.Click += new EventHandler(comboBox1_Click);
            //   this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
 
 
 
 
            //   this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
            //   this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
        }
 
        void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // MessageBox.Show("Test");
            //  this.comboBox1.Items.Clear();
            // this.comboBox1.Items.AddRange(listNames.Where(t => t.Contains(comboBox1.Text)).ToArray());
        }
 
        //void comboBox1_TextChanged(object sender, EventArgs e)
        //{
        //    MessageBox.Show("Changed");
        //}
 
        void comboBox1_TextUpdate(object sender, EventArgs e)
        {
 
            //MessageBox.Show("update");
 
            this.Cursor = Cursors.Arrow;
            dropDownHeight = 0;
            string search = comboBox1.Text.Trim();
            if (string.IsNullOrWhiteSpace(search))
            {
                comboBox1.Items.Clear();

                comboBox1.BeginUpdate();
                comboBox1.Items.AddRange(listNames.ToArray());
                comboBox1.EndUpdate();
 
                dropDownHeight = comboBox1.ItemHeight * ((listNames.Count) + 1);
                comboBox1.DropDownHeight = dropDownHeight;
                comboBox1.IntegralHeight = true;
                //  comboBox1.Refresh();
 
                //  comboBox1.SelectedIndex = -1;
                comboBox1.DroppedDown = true;
                this.Cursor = Cursors.Arrow;
 
                return;
            }
            var results = listNames.Where(t => t.Contains(search)).ToArray();
            if (results.Count() > 0)
            {
                comboBox1.Items.Clear();
                comboBox1.BeginUpdate();
                foreach (var item in results)
                {
                    comboBox1.Items.Add(item);
 
                }
                comboBox1.EndUpdate();
                dropDownHeight = comboBox1.ItemHeight * (results.Count() + 1);
                comboBox1.DropDownHeight = dropDownHeight;
                //  comboBox1.SelectedIndex = -1;
 
                comboBox1.DroppedDown = true;
                comboBox1.IntegralHeight = true;
 
 
                this.Cursor = Cursors.Arrow;
                comboBox1.SelectionStart = search.Length;
            }
            else
            {
 
                //   comboBox1.Items.Clear(); 
                comboBox1.DropDownHeight = 1;
                //comboBox1.DroppedDown = false;
            }
            this.Cursor = Cursors.Arrow;
            //  this.Cursor = this.DefaultCursor;
 
 
            //comboBox1.DropDownHeight = dropDownHeight;
 
 
 
        }
 
        void comboBox1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Test");
            comboBox1.Text = "ganquanfu";
        }
 
        void comboBox1_TextChanged(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Arrow;
            dropDownHeight = 0;
            string search = comboBox1.Text.Trim();
            if (string.IsNullOrWhiteSpace(search))
            {
                comboBox1.Items.Clear();
                comboBox1.Items.AddRange(listNames.ToArray());
                dropDownHeight = comboBox1.ItemHeight * (listNames.Count + 1);
                comboBox1.DropDownHeight = dropDownHeight;
                //    comboBox1.Refresh();
                comboBox1.DroppedDown = true;
                this.Cursor = Cursors.Arrow;
                return;
            }
            var results = listNames.Where(t => t.Contains(search)).ToArray();
            if (results.Count() > 0)
            {
                comboBox1.Items.Clear();
                foreach (var item in results)
                {
                    comboBox1.Items.Add(item);
 
                }
                dropDownHeight = comboBox1.ItemHeight * (results.Count() + 1);
                comboBox1.DropDownHeight = dropDownHeight;
                //  comboBox1.Refresh();
                comboBox1.DroppedDown = true;
                this.Cursor = Cursors.Arrow;
                comboBox1.SelectionStart = search.Length;
            }
            else
            {
 
                // comboBox1.Items.Clear(); 
                comboBox1.DropDownHeight = 1;
                comboBox1.DroppedDown = false;
            }
            this.Cursor = Cursors.Arrow;
 
 
        }
 
        void comboBox1_DropDownClosed(object sender, EventArgs e)
        {
            MessageBox.Show(this.comboBox1.DroppedDown.ToString());
 
        }
 
        void comboBox1_DropDown(object sender, EventArgs e)
        {
            // comboBox1.Items.AddRange(listNames);
            this.comboBox1.Items.Clear();
            this.comboBox1.Items.AddRange(listNames.Where(t => t.Contains(comboBox1.Text)).ToArray());
 
 
 
 
            MessageBox.Show("update");
 
            this.Cursor = Cursors.Arrow;
            dropDownHeight = 0;
            string search = comboBox1.Text.Trim();
            if (string.IsNullOrWhiteSpace(search))
            {
                comboBox1.Items.Clear();
                comboBox1.Items.AddRange(listNames.ToArray());
                dropDownHeight = comboBox1.ItemHeight * (listNames.Count + 1);
                comboBox1.DropDownHeight = dropDownHeight;
                comboBox1.Refresh();
 
                comboBox1.DroppedDown = true;
                this.Cursor = Cursors.Arrow;
 
                return;
            }
            var results = listNames.Where(t => t.Contains(search)).ToArray();
            if (results.Count() > 0)
            {
                comboBox1.Items.Clear();
                foreach (var item in results)
                {
                    comboBox1.Items.Add(item);
 
                }
                dropDownHeight = comboBox1.ItemHeight * (results.Count() + 1);
                comboBox1.DropDownHeight = dropDownHeight;
                comboBox1.Refresh();
                comboBox1.DroppedDown = true;
                this.Cursor = Cursors.Arrow;
                comboBox1.SelectionStart = search.Length;
            }
            else
            {
 
                comboBox1.Items.Clear();
                comboBox1.DropDownHeight = 1;
                comboBox1.DroppedDown = false;
            }
            this.Cursor = Cursors.Arrow;
 
 
            comboBox1.DropDownHeight = dropDownHeight;
 
 
 
 
 
 
        }
 
        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;
 
        }
 
        private void button1_Click_1(object sender, EventArgs e)
        {
            People<string> pString = new People<string>();
            pString.Show("ganquanfu");
            People<int> pInt = new People<int>();
            pInt.Show(200);
        }
    }
    class People<r>
    {
        public void Show(r @object)
        {
            MessageBox.Show(@object.ToString());
        }
    }
}
posted @ 2013-05-04 18:15  Predator  阅读(1033)  评论(0编辑  收藏  举报