ComboBox怎么实现模糊搜索
ComboBox如何实现模糊搜索
ComboBox如何实现模糊搜索?就像百度搜索框那样,输入一个字符,下拉框就显示包含该字符的列表。
------解决方案--------------------
得定义一个全局变量保存ComboBox1->Items的信息
------解决方案--------------------
ComboBox如何实现模糊搜索?就像百度搜索框那样,输入一个字符,下拉框就显示包含该字符的列表。
------解决方案--------------------
得定义一个全局变量保存ComboBox1->Items的信息
------解决方案--------------------
TStringList *p;
void __fastcall TForm1::FormCreate(TObject *Sender)
{
p = new TStringList();
p->Add("11"); // 把p作为来源容器最好
p->Add("22");
p->Add("33");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
delete p;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1KeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
if(Key == VK_RETURN)
{
String sTemp = ComboBox1->Text;
ComboBox1->Items->Clear();
int count = p->Count;
if(sTemp==""){
ComboBox1->Items->Text=p->Text;
}else{
bool oo=true;
for(int i = 0;i< p->Count;i++)
{
if(p->Strings[i].Pos(sTemp)>0)
{
ComboBox1->Items->Add(p->Strings[i]);
oo=false;
}
}
if(oo==true)
{
ComboBox1->Items->Add(sTemp);
}
}
}else
if(Key == VK_BACK)
{
for(int i = 0;i<p->Count;i++)
{
ComboBox1->Items->Add(p->Strings[i]);
}
}
}