Last week i learned about auto-complete property of text box.so i want to share it with all of you so that it can help you as well.
First declare following global variables.
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
private string strConnection =System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
SqlConnection conn = new SqlConnection();
After that i have made a function
public void AutoComplete()
{
SqlDataReader dReader;
conn.ConnectionString = strConnection;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from test";
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();
conn.Close();
textword.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textword.AutoCompleteSource = AutoCompleteSource.CustomSource;
textword.AutoCompleteCustomSource =collection;
}
Now call this function on the Form Load event.
private void Formname_Load(object sender, EventArgs e)
{
AutoComplete();
}
After that output will look like this.
Hope this post will help you.
Happy programming.....:)
First declare following global variables.
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
private string strConnection =System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
SqlConnection conn = new SqlConnection();
After that i have made a function
public void AutoComplete()
{
SqlDataReader dReader;
conn.ConnectionString = strConnection;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from test";
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();
conn.Close();
textword.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textword.AutoCompleteSource = AutoCompleteSource.CustomSource;
textword.AutoCompleteCustomSource =collection;
}
Now call this function on the Form Load event.
private void Formname_Load(object sender, EventArgs e)
{
AutoComplete();
}
After that output will look like this.
Hope this post will help you.
Happy programming.....:)
hmmm gud job!!!!!!!!
ReplyDelete