Tuesday, 6 November 2012

AUTO COMPLETE IN TEXTBOX IN WINDOWS


 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 Sql  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 MySql.Data.MySqlClient;

namespace auto_complete_textbox_example
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string strConnection = ("server=localhost;database=sample;uid=root;password=raavi@123;");
       AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();

        private void Form1_Load(object sender, EventArgs e)
        {

            string var = txt_username.Text;
            txt_username.Focus();
            //RePaint();
           MySqlDataReader dReader;
            MySqlConnection conn = new MySqlConnection();
            conn.ConnectionString = strConnection;
           MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
 
            cmd.CommandText = "Select distinct [UserName] from [tbl_login] order by [UserName] asce ";
            conn.Open();
            dReader = cmd.ExecuteReader();
            if (dReader.HasRows == true)
            {
            while (dReader.Read())
                namesCollection.Add(dReader["UserName"].ToString());
       
            }
            else
            {
            MessageBox.Show("Data Not Found");

            }
            dReader.Close();
            txt_username.AutoCompleteMode = AutoCompleteMode.Suggest;
            txt_username.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txt_username.AutoCompleteCustomSource = namesCollection;

        }
    }
}

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

Demo


2 comments:

  1. what is this "asce"??Its showing Error..

    ReplyDelete
  2. hello friends here use "asc" instead of "asce"...don't b confused..

    ReplyDelete