当前位置导航:炫浪网>>网络学院>>编程开发>>C++教程>>C++基础入门教程

实现ComboBox输入文字的自动完成

 word lastkey ;

//---------------------------------------------------------------------------
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 ;
}
//---------------------------------------------------------------------------

相关内容
赞助商链接