Click here to Skip to main content
11,922,026 members (38,123 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
I have a textbox and a database.Everytime the user types something in the textbox,data with string matching textbox's string is loaded from the database.

Now,how can i show this data in the AutoComplete Mode as it appears in Search Engines?

The database i'm using is SQL.
Posted 27-Dec-12 1:29am
Edited 27-Dec-12 1:35am
Asa code 11-Feb-14 23:10pm
also I want to know when select one item in text box and load to other column in table to another text box .... can some one help this ?
CHill60 1-Apr-14 19:53pm
Use the "Ask a question" link or search through CodeProject answers
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

This way...
private void Form1_Load(object sender, EventArgs e)
    // declare custom source.
    var source = new AutoCompleteStringCollection();
    // fetch record from database
    datatable dt = getdatafromsql("select name from tbl");
    // fill database records to custome sorurce
    for (i=0;i<dt.rows.count;i++)>
    // Create and initialize the text box.
    var textBox = new TextBox
                      AutoCompleteCustomSource = source,
                      AutoCompleteMode =
                      AutoCompleteSource =
                      Location = new Point(20, 20),
                      Width = ClientRectangle.Width - 40,
                      Visible = true
    // Add the text box to the form.
Happy Coding!
Smile | :)
Husain Sabir 27-Dec-12 7:09am
Got it.But i also want to scroll through the suggestions and then select a value.
Aarti Meswania 27-Dec-12 7:13am
okay then use an Auto complete Suggestion Combobox
Husain Sabir 27-Dec-12 7:16am
I know that.But i don't want to use a combobox.I want to implement it in a text box.
Aarti Meswania 27-Dec-12 7:21am
then you have to create your own control because as i found solution on internet I have not seen this control with scroll for winform

and if want scroll then you need a button to popup suggesstion and that button at right hand side makes your textbox like combo
Husain Sabir 27-Dec-12 7:31am
no no.ur not getting me.
i want it to be as in google search.where u get suggestions based on ur query and u can select ur relevant result from the suggestions.
Aarti Meswania 27-Dec-12 7:34am
I got you dear
see properly google suggestion will not show scroll!!! :)
Husain Sabir 27-Dec-12 7:40am
i didn't actually mean scroll.
but i just want that when i press the down key,the suggestion should be highlighted and when i press enter,the highlighted suggestion should be selected in the text box.
Aarti Meswania 27-Dec-12 7:44am
that will possible if done after write some char in textbox
winform auto suggestion textbox is having same behavior as google search textbox provides

so only way is using combobox
Husain Sabir 27-Dec-12 8:00am
ok.thank you!
Aarti Meswania 27-Dec-12 8:00am
Welcome! :)
Glad to help you! :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Try this

public void AutoComplete() 
AutoCompleteStringCollection AutoItem = new AutoCompleteStringCollection();
foreach (DataRow rw in Dt.rows)
texbox.AutoCompleteMode = AutoCompleteMode.Suggest;
texbox.AutoCompleteSource = AutoCompleteSource.CustomSource;
texbox.AutoCompleteCustomSource = AutoItem;
Husain Sabir 27-Dec-12 6:55am
This works.But i also want to select from the suggestions.
Soner Gönül 27-Aug-14 8:36am
Then you can assing AutoCompleteMode property as well.
Rate this: bad
Please Sign up or sign in to vote.

Solution 6

Sample code

textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
getData(DataCollection); //add your database values here
textBox1.AutoCompleteCustomSource = DataCollection;

Full Source : Autocomplete Textbox


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 493
1 Dave Kreskowiak 285
2 Sergey Alexandrovich Kryukov 243
3 Richard MacCutchan 155
4 ppolymorphe 135
0 OriginalGriff 7,231
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,356
3 George Jonsson 2,865
4 Suvendu Shekhar Giri 2,216

Advertise | Privacy | Mobile
Web03 | 2.8.151120.1 | Last Updated 8 Jul 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100