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=.;i Nitial catalog=testdb;integrated security=true ");
14. Public autocomplete ()
15. {
16. Conn. Open ();
17. SqlCommand cmd = new SqlCommand ("SELECT * from AutoComplete", Conn);
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. {
Panax Notoginseng. 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 (); }
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. {
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.}