程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#實現百度中的搜索框的下選效果

C#實現百度中的搜索框的下選效果

編輯:關於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.}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved