C#实现百度中的搜索框的下选效果!

01.using System;   
02.using System.Collections.Generic;   
03.using System.Text;   
04.using System.Windows.Forms;   
05.using System.Data.SqlClient;   
06. 
07.namespace NewApp   
08.{   
09.    class AutoComplete   
10.    {   
11.        List<TextBox> _CompleteObjectList = new List<TextBox>();   
12.        Dictionary<string, AutoCompleteStringCollection> _Source = new Dictionary<string, AutoCompleteStringCollection>();   
13.        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=TestDB;Integrated Security=True");   
14.        public AutoComplete()   
15.        {   
16.            conn.Open();   
17.            SqlCommand cmd = new SqlCommand("select * from AutoComplete", conn);   
18.            SqlDataReader read = cmd.ExecuteReader();   
19.            while (read.Read())   
20.            {   
21.                string key = read["name"].ToString();   
22.                if (!_Source.ContainsKey(key))   
23.                    _Source.Add(key, new AutoCompleteStringCollection());   
24.                _Source[key].Add(read["str"].ToString());   
25.            }   
26.            read.Close();   
27.            conn.Close();   
28.        }   
29. 
30.        public void AddAll(Control item)   
31.        {   
32.            for (int i = 0; i < item.Controls.Count; i++)   
33.            {   
34.                Control var = item.Controls[i];   
35.                if (var.GetType().Equals(typeof(TextBox)))   
36.                {   
37.                    Add(var as TextBox);   
38.                }   
39.            }   
40.        }   
41. 
42.        public void Add(TextBox text)   
43.        {   
44.            _CompleteObjectList.Add(text);   
45.            text.Leave += new EventHandler(text_Leave);   
46.            text.AutoCompleteMode = AutoCompleteMode.SuggestAppend;   
47.            text.AutoCompleteSource = AutoCompleteSource.CustomSource;   
48.            if (!_Source.ContainsKey(text.Name))   
49.            {   
50.                _Source.Add(text.Name, new AutoCompleteStringCollection());   
51.            }   
52.            text.AutoCompleteCustomSource = _Source[text.Name];   
53.        }   
54. 
55.        public void Delete(TextBox text)   
56.        {   
57.            _CompleteObjectList.Remove(text);   
58.        }   
59. 
60.        public void DeleteAll(Control item)   
61.        {   
62.            for (int i = 0; i < item.Controls.Count; i++)   
63.            {   
64.                Control var = item.Controls[i];   
65.                if (var.GetType().Equals(typeof(TextBox)))   
66.                {   
67.                    Delete(var as TextBox);   
68.                }   
69.            }   
70.        }   
71. 
72.        public void AutoCompleteClear()   
73.        {   
74.            foreach (AutoCompleteStringCollection var in _Source.Values)   
75.            {                var.Clear();            }   
76.            SqlCommand cmd = new SqlCommand("Delete AutoComplete", conn);   
77.            conn.Open();   
78.            cmd.ExecuteNonQuery();   
79.            conn.Close();   
80.        }   
81. 
82.        void text_Leave(object sender, EventArgs e)   
83.        {   
84.            TextBox text = sender as TextBox;   
85.            if (text.Text == "")   
86.                return;   
87.            string key = text.Name;   
88.            if (!_Source.ContainsKey(key))   
89.            {   
90.                _Source.Add(key, new AutoCompleteStringCollection());   
91.            }   
92.            if (!_Source[key].Contains(text.Text))   
93.            {   
94.                SqlCommand cmd = new SqlCommand("insert into AutoComplete select '" + key.Replace("'", "''") + "', '" + text.Text.Replace("'", "''") + "'", conn);   
95.                conn.Open();   
96.                cmd.ExecuteNonQuery();   
97.                _Source[key].Add(text.Text);   
98.                conn.Close();   
99.            }   
100.        }   
101.    }   
102.} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值