Monday, 19 August 2013

AutoComplete Textbox in Windows Application C#.Net


To implement the AutoComplete Textbox in Windows form C#.net  you need to follow the below steps :

Step1 :
First you need to design a table in Ms Access 2007  to retrieve the records from database.

Step2:
On the
 File menu, click New Project. Select Windows Forms Application as your project type.
Design the Form using controls from Toolbox.

Step3:
Now open the Form.cs page and write the following source code

 Form.cs Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Linq;
using System.Text;
using System.Windows.Forms; 
using System.Data.OleDb;

namespace Autocomplete
{


    public partial class Autocomplete_TextBox : Form

    {

        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\App_Data\SampleDB.accdb; Jet OLEDB:Database Password=1234;Persist Security Info=False");
        AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();

        public AmirAds_MailSend()
        {
            InitializeComponent();
        }

         private void Autocomplete_TextBox_Load(object sender, EventArgs e)
        {
            con.Open();
            OleDbCommand cmd = new OleDbCommand("Select distinct Email_Id from Sample_CreateClient order by Email_Id asc", con);
            cmd.CommandType = CommandType.Text;
            OleDbDataReader dr;
            dr= cmd.ExecuteReader();
            if (dr.HasRows == true)
            {
                while (dr.Read())
                    namesCollection.Add(dr["Email_Id"].ToString());
            }
            con.Close();

            txtToMail.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtToMail.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtToMail.AutoCompleteCustomSource = namesCollection;

        }
    }
}

  
Step4:
Now build the Solution and Debug it for the output.


No comments:

Post a Comment