using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Auto_Complete_ComboBox
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmAutoCompleteComboBox : System.Windows.Forms.Form
{
internal System.Windows.Forms.Label lblTest;
internal System.Windows.Forms.CheckBox chkLimitToList;
internal System.Windows.Forms.CheckBox chkAutoComplete;
internal System.Windows.Forms.ComboBox cbAutoComplete;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmAutoCompleteComboBox()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblTest = new System.Windows.Forms.Label();
this.chkLimitToList = new System.Windows.Forms.CheckBox();
this.chkAutoComplete = new System.Windows.Forms.CheckBox();
this.cbAutoComplete = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// lblTest
//
this.lblTest.AutoSize = true;
this.lblTest.Location = new System.Drawing.Point(8, 151);
this.lblTest.Name = "lblTest";
this.lblTest.Size = new System.Drawing.Size(0, 16);
this.lblTest.TabIndex = 7;
//
// chkLimitToList
//
this.chkLimitToList.Checked = true;
this.chkLimitToList.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkLimitToList.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.chkLimitToList.Location = new System.Drawing.Point(208, 39);
this.chkLimitToList.Name = "chkLimitToList";
this.chkLimitToList.Size = new System.Drawing.Size(80, 16);
this.chkLimitToList.TabIndex = 2;
this.chkLimitToList.Text = "Limit to List";
//
// chkAutoComplete
//
this.chkAutoComplete.Checked = true;
this.chkAutoComplete.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkAutoComplete.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.chkAutoComplete.Location = new System.Drawing.Point(208, 15);
this.chkAutoComplete.Name = "chkAutoComplete";
this.chkAutoComplete.Size = new System.Drawing.Size(96, 16);
this.chkAutoComplete.TabIndex = 1;
this.chkAutoComplete.Text = "Auto Complete";
//
// cbAutoComplete
//
this.cbAutoComplete.Items.AddRange(new object[] {
"Aaron Alex",
"Zernik Uri",
"Ziarko Wojciech P.",
"Zicari Roberto"});
this.cbAutoComplete.Location = new System.Drawing.Point(16, 15);
this.cbAutoComplete.MaxDropDownItems = 10;
this.cbAutoComplete.Name = "cbAutoComplete";
this.cbAutoComplete.Size = new System.Drawing.Size(176, 21);
this.cbAutoComplete.TabIndex = 0;
this.cbAutoComplete.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.cbAutoComplete_KeyPress);
//
// frmAutoCompleteComboBox
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(312, 182);
this.Controls.Add(this.lblTest);
this.Controls.Add(this.chkLimitToList);
this.Controls.Add(this.chkAutoComplete);
this.Controls.Add(this.cbAutoComplete);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "frmAutoCompleteComboBox";
this.Text = "Auto Complete ComboBox - Example";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmAutoCompleteComboBox());
}
private void cbAutoComplete_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (this.chkAutoComplete.Checked)
this.AutoComplete(this.cbAutoComplete, e, this.chkAutoComplete.Checked);
}
// AutoComplete
public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e)
{
this.AutoComplete(cb, e, false);
}
public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e, bool blnLimitToList)
{
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 = blnLimitToList;
}
}
}
}