PDA

View Full Version : automatic text completion for a ComboBox



r00tkit
پنج شنبه 13 اسفند 1388, 12:33 عصر
سلام

(در جواب یکی از دوستان)
در این پست چگونگی تکمیل شدن خود کار text در combobox رو توضیح می دم
شما می تونین هم با type و هم با click مقدار ها در لیست به combobox مقدار بدین

وقتی شما چیزی رو تایپ می کنید اگه خود combobox متن شما رو تکمیل کنه چقدر سرعت بالا می ره از این امکان خیلی جا ها استفاده شده مثل IntelliSense بودن visual stadio و....

برای ادامه تو یه پروژه ی یه combobox اضافه کنید و کد ها ی زیر رو تو Load event فورم اضافه کنید:


this.comboBox1.Text = ""; this.comboBox1.Items.Add("a");
this.comboBox1.Items.Add("aaa"); this.comboBox1.Items.Add("combo");
this.comboBox1.Items.Add("combobox"); this.comboBox1.Items.Add("combobox test");
this.comboBox1.Items.Add("common"); this.comboBox1.Items.Add("common dialog");


حالا کد های زیر رو تو KeyUp event ComboBox اضافه کنید



int index;
string actual;
string found;

// Do nothing for certain keys, such as navigation keys.
if ((e.KeyCode == Keys.Back) ||
(e.KeyCode == Keys.Left) ||
(e.KeyCode == Keys.Right) ||
(e.KeyCode == Keys.Up) ||
(e.KeyCode == Keys.Down) ||
(e.KeyCode == Keys.Delete) ||
(e.KeyCode == Keys.PageUp) ||
(e.KeyCode == Keys.PageDown) ||
(e.KeyCode == Keys.Home) ||
(e.KeyCode == Keys.End))
{
return;
}

// Store the actual text that has been typed.
actual = this.comboBox1.Text;

// Find the first match for the typed value.
index = this.comboBox1.FindString(actual);

// Get the text of the first match.
if (index > -1)
{
found = this.comboBox1.Items[index].ToString();

// Select this item from the list.
this.comboBox1.SelectedIndex = index;

// Select the portion of the text that was automatically
// added so that additional typing replaces it.
this.comboBox1.SelectionStart = actual.Length;
this.comboBox1.SelectionLength = found.Length;
}