Thursday, 18 October 2012

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.

9 comments:

  1. thanx sir it work but it has a problem u have to keep your database in solution but if we want to add new record in it it not work we want that database reside in sql server & we access auto complete textbox from sql which isb not possible here.........reply me on
    chaudharysunny1989@gmail.com

    ReplyDelete
    Replies
    1. Hi Sunny Saharawat

      In the above example i have used the MSAccess database. If you are using SQL Server database then use this code.

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows.Forms;
      using System.Data.SqlClient;

      namespace WindowsFormsApp
      {
      public partial class Form1 : Form
      {
      SqlConnection con = new SqlConnection(@"Data Source=TR\SQLEXP;Initial Catalog=Employee;User ID=sa;Password=123");
      AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();

      public Form1()
      {
      InitializeComponent();
      }

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

      txtLocation.AutoCompleteMode = AutoCompleteMode.Suggest;
      txtLocation.AutoCompleteSource = AutoCompleteSource.CustomSource;
      txtLocation.AutoCompleteCustomSource = namesCollection;
      }
      }
      }


      Delete
    2. BUt this will populate only the very first time. If u enter semi colon and then try entering the next email id,it wont populate.

      Delete
  2. hi sir,
    I have an issue that is how can we separate mail Ids when we add more than one mail Ids using auto complete in c #window application(Infragistic tool)

    Please reply me..

    ReplyDelete
  3. Replies
    1. The code is perfect, it is tested and posted. Please check your code.

      Delete
    2. sridhar , BUt this will populate only for the very first time when the user enters some value in textbox. once the first value is auto completed and If u enter semi colon and then try entering the next email id,it wont populate.

      Delete
  4. Attempted to read write and protected memory problem i am facing when typing in the textbox.please reply on my email id manpreettwinkle0@gmail.com

    ReplyDelete

CRUD operations using AngularJS in Asp.Net MVC

In this article I will show you how to perform CRUD operations in Asp.Net MVC5 using AngularJS. Add AngularJS using the Manage Nuget Pack...