You are on page 1of 2

private void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e) { string strFindStr = ""; if (e.KeyChar == (char)8) { if (cb.SelectionStart <= 1) { cb.

Text = ""; return; } if (cb.SelectionLength == 0) strFindStr = cb.Text.Substring(0, cb.Text.Length - 1); else strFindStr = cb.Text.Substring(0, cb.SelectionStart - 1); } else { if (cb.SelectionLength == 0) strFindStr = cb.Text + e.KeyChar; else strFindStr = cb.Text.Substring(0, cb.SelectionStart) + e.KeyChar; } int intIdx = -1; // Search the string in the ComboBox list. intIdx = cb.FindString(strFindStr); if (intIdx != -1) { cb.SelectedText = ""; cb.SelectedIndex = intIdx; cb.SelectionStart = strFindStr.Length; cb.SelectionLength = cb.Text.Length; e.Handled = true; } else { e.Handled = true; } } private void cboten_KeyPress(object sender, KeyPressEventArgs e) { this.AutoComplete(cboten, e); } private void cboten_Enter(object sender, EventArgs e) { cboten.DataSource = this.bindingSourcesp; cboten.DisplayMember = "TEN_HANG"; cboten.ValueMember = "MA_HANG"; } private void cboten_Leave(object sender, EventArgs e) { cboten.DataSource = null;

You might also like