Welcome to my blog which contains anything and everything I feel is worthwhile sharing......

Tuesday, March 27, 2012

Creating an auto-complete box C#

Create a Standard C# Form and add a combo box.

The following method initializes the combo box with the auto-complete functionality.

public void init_box()
{
AutoCompleteStringCollection ax = new AutoCompleteStringCollection();
if (File.Exists("List.txt"))
{
using (StreamReader reader = new StreamReader("List.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
ax.Add(line);
}
}
}
comboBox1.AutoCompleteCustomSource = ax;
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

}

next, this method is called on the mouse click event of the combo box.

private void comboBox1_MouseClick(object sender, MouseEventArgs e)
{
init_box();
}


No comments:

Post a Comment