Wednesday, March 10, 2010

Auto-complete feature in .net for winforms controls

Many a times it is required for us to use the auto-complete feature in the controls of the winforms .net application. This feature of auto-complete can be applied both to textbox and combobox alike. The code for it in c#.net is as follows:

public void AutoCompleteTextBox(string tableName, string fieldName, TextBox txtToAutoComp)
        {
            AutoCompleteStringCollection txtCollection = new AutoCompleteStringCollection();
            DataTable dtAutoComp = Dal.ExecuteSP("StoredProcedure", fieldName, tableName);
            if (dtAutoComp.Rows.Count >= 0)
            {
                for (int count = 0; count < dtAutoComp.Rows.Count; count++)
                {
                    txtCollection.Add(dtAutoComp.Rows[count][fieldName].ToString());
                }
            }
            txtToAutoComp.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtToAutoComp.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtToAutoComp.AutoCompleteCustomSource = txtCollection;
        }
For implementing the code for combobox just replace the textbox variable with combobox variable. Everything else remains same.

10 comments:

  1. Thanks Jaivardhan for the code... it really helped me a lot. Now I can show the users what all data relating to their entry is already in the database.

    ReplyDelete
  2. get the word repair utility to fix data corruption issues in the files of specified format

    ReplyDelete
  3. Wow! Great in depth guide with so many details. I love how you went into things step by step. Great piece.

    ReplyDelete
  4. Exceptional work! I'm going to read more of your posts after reading this article. Also visit here: Feeta.pk pakistani real estate . Maintain your good work.

    ReplyDelete

Comments to this post

LinkWithin

Related Posts with Thumbnails