C # implement the search box Selection in Baidu!

Source: Internet
Author: User

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/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.