Click here to Skip to main content
11,790,613 members (63,710 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# WinForm
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 0:29am
Edited 27-Dec-12 0:35am
Asa code at 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 at 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 at 27-Dec-12 7:09am
Got it.But i also want to scroll through the suggestions and then select a value.
Aarti Meswania at 27-Dec-12 7:13am
okay then use an Auto complete Suggestion Combobox
Husain Sabir at 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 at 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 at 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 at 27-Dec-12 7:34am
I got you dear
see properly google suggestion will not show scroll!!! :)
Husain Sabir at 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 at 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 at 27-Dec-12 8:00am
ok.thank you!
Aarti Meswania at 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 at 27-Dec-12 6:55am
This works.But i also want to select from the suggestions.
Soner Gönül at 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 1,114
1 Maciej Los 825
2 KrunalRohit 686
3 CPallini 611
4 ppolymorphe 610

Advertise | Privacy | Mobile
Web02 | 2.8.1509028.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