Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Updated 27-Dec-12 0:35am
v3
Comments
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

Try this

C#
public void AutoComplete() 
{
AutoCompleteStringCollection AutoItem = new AutoCompleteStringCollection();
foreach (DataRow rw in Dt.rows)
{
AutoItem.Add(Rw["columnname"].ToString());

}
texbox.AutoCompleteMode = AutoCompleteMode.Suggest;
texbox.AutoCompleteSource = AutoCompleteSource.CustomSource;
texbox.AutoCompleteCustomSource = AutoItem;
}
 
Share this answer
 
v2
Comments
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.
This way...
C#
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++)>
    {
         source.Add(dt.rows[i][0].tostring(););
    }
    // Create and initialize the text box.
    var textBox = new TextBox
                  {
                      AutoCompleteCustomSource = source,
                      AutoCompleteMode =
                          AutoCompleteMode.SuggestAppend,
                      AutoCompleteSource =
                          AutoCompleteSource.CustomSource,
                      Location = new Point(20, 20),
                      Width = ClientRectangle.Width - 40,
                      Visible = true
                  };

    // Add the text box to the form.
    Controls.Add(textBox);
}

Happy Coding!
:)
 
Share this answer
 
Comments
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
visit...
http://www.codeproject.com/Articles/11899/Auto-Complete-Multi-Column-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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900