日期:2014-05-18  浏览次数:20814 次

ComboBox索引问题
ComboBox的item中的数据为
1   苹果
2   李子
3   梨
4   香蕉
注:1和苹果之间有空格
我想实现的是当我输入“1”时,combobox显示“1   苹果”,当在combobox中输入“苹果”的时候也显示”1   苹果“,怎么实现呢?

------解决方案--------------------
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
for (int i = 0; i < this.comboBox1.Items.Count; i++)
{
string strItem = this.comboBox1.Items[i] as string;
if (strItem != null && strItem.IndexOf(this.comboBox1.Text) > -1)
{
this.comboBox1.SelectedIndex = i;
break;
}
}
}
}
------解决方案--------------------
这样做吧:

private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
int index = -1;
for (int i = 0; i < this.comboBox1.Items.Count; i++)
{
string strItem = this.comboBox1.Items[i] as string;
if (strItem != null && strItem.IndexOf(e.KeyChar.ToString()) > -1)
{
index = i;
break;
}
}
if (index != -1)
{
this.comboBox1.SelectedIndex = index;
e.Handled = true;
}
}