Thursday, 21 March 2013

Autocomplete textbox in c# windows application...


 take a form with a textbox named txtname. Here I am fetching names from the database.



public partial class Form9 : Form
{ public string strConnection = ConfigurationManager.ConnectionStrings["test1"].ConnectionString;
 AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
public Form9()
{
InitializeComponent();
}private void Form9_Load(object sender, EventArgs e)
{OdbcDataReader dReader;
OdbcConnection conn = new
OdbcConnection();
conn.ConnectionString = strConnection;OdbcCommand cmd = new OdbcCommand();
cmd.Connection = conn;cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select distinct name from sample order by name";
conn.Open();
dReader = cmd.ExecuteReader();if (dReader.HasRows == true)
{while (dReader.Read())
namesCollection.Add(dReader["name"].ToString());
}else
{MessageBox.Show("Data not found");
}
dReader.Close();txtname.AutoCompleteMode = AutoCompleteMode.Suggest;
txtname.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtname.AutoCompleteCustomSource = namesCollection;
}
}
Google