WORD lastkey ;(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
String value = ComboBox1-Text ;
// If the user tried to delete he must not want to change anything.
if (lastkey == 'b' || lastkey == VK_DELETE)
{
lastkey = 0 ;
return ;
}
lastkey = 0 ;
// 确保使用者没有在中间插入字符
if (ComboBox1-SelStart != value.Length ())
return ;
// 在下拉列表中寻找匹配项.
int index = SendMessage (ComboBox1-Handle, CB_FINDSTRING, -1, (LPARAM) value.c_str ()) ;
if (index = 0)
{
// 找到匹配项并显示.
ComboBox1-ItemIndex = index ;
String newtext = ComboBox1-Text ;
SendMessage (ComboBox1-Handle, CB_SETEDITSEL, 0, MAKELPARAM (value.Length (), -1)) ;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
// 按下的最后一个键值.
lastkey = Key ;
}
//---------------------------------------------------------------------------