First, lets bind the combo with some data:
protected void FillCombo()
{
try
{
String strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Power\\Desktop\\db.mdb";
OleDbConnection conn = new OleDbConnection(strconn);
conn.Open();
DataSet ds = new DataSet();
String strSQL;
OleDbCommand cmd;
OleDbDataAdapter da = new OleDbDataAdapter();
// Fill dataset
// DataRow dr;
DataTable dt;
da.TableMappings.Add("Table", "Positions");
strSQL = "Select * From Positions";
cmd = new OleDbCommand(strSQL, conn);
cmd.CommandType = CommandType.Text;
da.SelectCommand = cmd;
da.Fill(ds);
da.Dispose();
ds.AcceptChanges();
dt = ds.Tables[0];
ds.Dispose();
conn.Close();
//Fill Combo
this.cbPositions.Text = "";
this.cbPositions.Items.Clear();
this.cbPositions.BeginUpdate();
//Load Postisions into the ComBox Control
foreach (DataRow dr in dt.Rows)
{
this.cbPositions.Items.Add(dr["Position"].ToString());
}
this.cbPositions.EndUpdate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Add the KeyPressEventHandler to you Designer.cs file:
// cbPositions
//
this.cbPositions.FormattingEnabled = true;
this.cbPositions.Location = new System.Drawing.Point(55, 133);
this.cbPositions.Name = "cbPositions";
this.cbPositions.Size = new System.Drawing.Size(121, 21);
this.cbPositions.TabIndex = 0;
this.cbPositions.KeyPress += new System.Windows.Forms.KeyPressEventHandler(cbPositions_KeyPress);//add this line
Finally, add the cbPositions_KeyPress Event:
private void cbPositions_KeyPress(object sender,KeyPressEventArgs e)
{
cbPositions.DroppedDown =true;
if (char.IsControl(e.KeyChar ))
{
return ;
}
string str = cbPositions.Text.Substring( 0,cbPositions.SelectionStart) + e.KeyChar;
Int32 index= cbPositions.FindStringExact(str);
if ( index ==-1)
{
index = cbPositions.FindString(str);
}
this.cbPositions.SelectedIndex =index;
this.cbPositions.SelectionStart =str.Length ;
this.cbPositions.SelectionLength = this.cbPositions.Text.Length - this.cbPositions.SelectionStart;
e.Handled =true;
}
Happy Coding!




