Using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. windows. forms; <br/> using system. data. sqlclient; </P> <p> namespace newapp <br/> {<br/> class AutoComplete <br/> {<br/> List <textbox> _ completeobjectlist = new list <textbox> (); <br/> dictionary <string, autocompletestringcollection> _ source = new dictionary <string, autocompletestringcollection> (); <br/> SQL Connection conn = new sqlconnection ("Data Source = .; initial catalog = testdb; Integrated Security = true "); <br/> Public AutoComplete () <br/>{< br/> Conn. open (); <br/> sqlcommand cmd = new sqlcommand ("select * From AutoComplete", Conn); <br/> sqldatareader READ = cmd. executereader (); <br/> while (read. read () <br/>{< br/> string key = read ["name"]. tostring (); <br/> If (! _ Source. containskey (key) <br/> _ source. add (Key, new autocompletestringcollection (); <br/> _ source [Key]. add (read ["str"]. tostring (); <br/>}< br/> Read. close (); <br/> Conn. close (); <br/>}</P> <p> Public void addall (Control item) <br/>{< br/> for (INT I = 0; I <item. controls. count; I ++) <br/>{< br/> Control Var = item. controls [I]; <br/> If (var. getType (). equals (typeof (textbox) <br/>{< br/> Dd (VaR as textbox); <br/>}</P> <p> Public void add (textbox text) <br/>{< br/> _ completeobjectlist. add (text); <br/> text. leave + = new eventhandler (text_leave); <br/> text. autocompletemode = autocompletemode. suggestappend; <br/> text. autocompletesource = autocompletesource. customsource; <br/> If (! _ Source. containskey (text. name) <br/>{< br/> _ source. add (text. name, new autocompletestringcollection (); <br/>}< br/> text. autocompletecustomsource = _ source [text. name]; <br/>}</P> <p> Public void Delete (textbox text) <br/>{< br/> _ completeobjectlist. remove (text); <br/>}</P> <p> Public void deleteall (Control item) <br/>{< br/> for (INT I = 0; I <item. controls. count; I ++) <br/>{< br/> contro L var = item. controls [I]; <br/> If (var. getType (). equals (typeof (textbox) <br/>{< br/> Delete (VaR as textbox ); <br/>}</P> <p> Public void autocompleteclear () <br/> {<br/> foreach (autocompletestringcollection VaR in _ source. values) <br/> {var. clear () ;}< br/> sqlcommand cmd = new sqlcommand ("delete AutoComplete", Conn); <br/> Conn. open (); <br/> cmd. executenonquery (); <br/> con N. close (); <br/>}</P> <p> void text_leave (Object sender, eventargs E) <br/>{< br/> textbox text = sender as textbox; <br/> If (text. TEXT = "") <br/> return; <br/> string key = text. name; <br/> If (! _ Source. containskey (key) <br/>{< br/> _ source. add (Key, new autocompletestringcollection (); <br/>}< br/> If (! _ Source [Key]. contains (text. text) <br/>{< br/> sqlcommand cmd = new sqlcommand ("insert into AutoComplete select '" + key. replace ("'", "'' ") +"', '"+ text. text. replace ("'", "'' ") +"' ", Conn); <br/> Conn. open (); <br/> cmd. executenonquery (); <br/> _ source [Key]. add (text. text); <br/> Conn. close (); <br/>}< br/>}