My response to BulletVictim was incorrect. The AutoComplete functionality of the TextBox takes care of the character-by-character input of the user. And BulletVictim is correct in suggesting that you need a Leave event handler.
The solution that I provide is somewhat like the MSDN documentation TextBox.AutoCompleteSource Property [^].
First the code you seek:
using System;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace AutoComplete
{
public partial class Form1 : Form
{
TextBox autocomplete_textbox = null;
public Form1 ( )
{
InitializeComponent ( );
if ( autocomplete_textbox != null )
{
autocomplete_textbox.Dispose ( );
autocomplete_textbox = null;
}
}
TextBox create_autocomplete_textbox (
AutoCompleteStringCollection source )
{
TextBox text_box = new TextBox
{
AutoCompleteCustomSource = source,
AutoCompleteMode =
AutoCompleteMode.SuggestAppend,
AutoCompleteSource =
AutoCompleteSource.CustomSource,
Location = new Point ( 20, 20 ),
Width = ( ClientRectangle.Width - 40 ),
Visible = true
};
return ( text_box );
}
#if SQL
private void Form1_Load ( object sender,
EventArgs e )
{
AutoCompleteStringCollection names;
string command_text;
string connection_string;
DataTable data_table;
SqlCommand sql_command;
SqlConnection sql_connention;
SqlDataAdapter sql_data_adapter;
connection_string = @"Database Source.....";
sql_connention = new SqlConnection ( connection_string );
sql_connention.Open ( );
command_text = "select * from table1";
sql_command = new SqlCommand ( command_text,
sql_connention );
sql_data_adapter = new SqlDataAdapter ( sql_command );
data_table = new DataTable ( );
sql_data_adapter.Fill ( data_table );
names = new AutoCompleteStringCollection ( );
foreach ( DataRow data_row in data_table.Rows )
{
names.Add ( data_row [ "name" ].ToString ( ) );
}
autocomplete_textbox = create_autocomplete_textbox (
names );
autocomplete_textbox.Leave +=
new EventHandler (
autocomplete_textbox_Leave );
this.Controls.Add ( autocomplete_textbox );
}
#else
private void Form1_Load ( object sender,
EventArgs e )
{
var months = new AutoCompleteStringCollection ( );
months.AddRange ( new string [ ] { "January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
} );
autocomplete_textbox = create_autocomplete_textbox (
months );
autocomplete_textbox.Leave +=
new EventHandler (
autocomplete_textbox_Leave );
this.Controls.Add ( autocomplete_textbox );
}
#endif
void autocomplete_textbox_Leave ( object sender,
EventArgs e )
{
string chosen_name = String.Empty;
TextBox text_box = ( TextBox ) sender;
chosen_name = text_box.Text;
MessageBox.Show ( "Chosen: " + chosen_name );
}
private void exit_BUT_Click ( object sender,
EventArgs e )
{
Application.Exit ( );
}
}
}
There are two Form1_Load event handlers. Only one is compiled depending on whether or not the symbol SQL is defined. I added the directives because I do not know your connection string. If you remove the C# preprocessor directives, make sure that you remove the second Form1_Load event handler.
Leaving the code the way it is, all you need is a Form (Form1) and an exit button (exit_BUT) on your form. Leave room at the top for the autocomplete textbox.
I was surprised that I needed to create the TextBox dynamically. But that was my initial problem.
Hope that helps.