using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
namespace WinAutoComplete
{
public partial class Form1 : Form
{
AutoCompleteStringCollection ProductList = new
AutoCompleteStringCollection();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//declare connection string
string cnString = @"Data Source=(local); Initial Catalog=northwind;" +
"User Id=northwind;Password=northwind";
/*use following if you use standard security
string cnString = @"Data Source=(local);Initial
Catalog=northwind; Integrated Security=SSPI"; */
//declare Connection, command and other related objects
SqlConnection conGetData = new SqlConnection(cnString);
SqlCommand cmdGetData = new SqlCommand();
SqlDataReader drGetData;
try
{
//open connection
conGetData.Open();
//prepare connection object to get the data through
//reader and populate into dataset
cmdGetData.CommandType = CommandType.Text;
cmdGetData.Connection = conGetData;
cmdGetData.CommandText = "Select ProductName From Products";
//read data from command object
drGetData = cmdGetData.ExecuteReader();
if (drGetData.HasRows == true)
{
while (drGetData.Read())
ProductList.Add(drGetData["ProductName"].ToString());
}
else
MessageBox.Show("No data found in Products tables");
//close reader and connection
drGetData.Close();
conGetData.Close();
//set the default pattern to SuggestAppend
comboBoxPattern.SelectedIndex = 2;
txtProductID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtProductID.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtProductID.AutoCompleteCustomSource = ProductList;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//check if connection is still open then attempt to close it
if (conGetData.State == ConnectionState.Open)
{
conGetData.Close();
}
}
}
private void comboBoxPattern_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxPattern.Text)
{
case "Suggest":
txtProductID.AutoCompleteMode = AutoCompleteMode.Suggest;
break;
case "Append":
txtProductID.AutoCompleteMode = AutoCompleteMode.Append;
break;
case "SuggestAppend":
txtProductID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
break;
}
}
}
}
Let the show begin!
Hurry! We're done with all the hard work, its time to reap the fruit. Let's build the project and see if your project looks some what same as shown in Figure 1. So, fingers crossed? And ready to hit F5 on your keyboard?
I assume at this moment that your code has no compile level error which will prevent the build to happen. Now, go ahead and check how the result looks like. To me, I can only imagine two typical scenarios; either you'll be throwing your hands in the air and yell YES, or a Silent whisper, Ops.
In both the cases I'm with you. If you said YES, then congratulation, you've done it. This is just the beginning, soon you'll find yourself cracking heck of cool AutoComplete enabled data entry controls. Now, if you've said Ops, then nothing to panic, just simply make sure you have done all the steps. In case of major issue, just start from scratch.
Append
If we use this pattern of AutoComplete then end-user will see the suggested entry appended as each key is pressed. For example if you type: "to" you will get "tofu". See Figure 6 for example of Append pattern.
Figure: 6 - Example of Append Pattern
Suggest
If we use this pattern of AutoComplete then end-user will see the suggested entry is displayed with narrowed down choices as each key is pressed. However, the product ID field will not be appended. For example if you type: "to" you will not get "tofu" appended to Product ID textbox. See Figure 7 for example of Suggest pattern.
Figure: 7 - Example of Suggest Pattern
SuggestAppend
SuggestAppend pattern is combination of both Suggest and Append. Please see Figure 1 for the example of this pattern.
Conclusion
As you can see, a simple feature like this can go a long way to win trust of your end-users and improve user experience. Apart from providing custom source, you can see that in build AutoComplete features like File System is also handy to help the data entry done easily.
Thank you for reading; I sincerely hope this article will help you a bit or two to know about user interface enhancement. Feel free to drop me a line with your comments/suggestion at [email protected].