再问一个问题,怎么做成combobox的自动填充

道素 2003-04-04 10:34:27
当用户再我的combobox输入内容时,比如ax
这时我希望再combobox下拉列表中列出所有ax开头的字段,其他无关不用列出就像ie的地址栏
我用DroppedDown=true,combobox是列出所有的
我有用到了
SendMessage(cmbCartNo->Handle, CB_FINDSTRING,
cmbCartNo->ItemIndex, (long)cmbCartNo->Text.c_str());
但是都没有达到我想要的效果

还有一点补充,自动填充后,将光标定位到我输入内容的最后,后面自动填充的内容要处于选中状态,好不好实现

实在不行我就只有自己做控件了
...全文
232 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
道素 2003-04-07
  • 打赏
  • 举报
回复
我找到办法了:
In order to add autocomplete features to a TComboBox, you need to trap the OnKeyDown and
OnChange events. In these events you will do all of the searching, replacing, and selecting.
The Win32 ComboBox control actually _does_ have autocomplete built into it, but it is not
always implemented. To help you out with this feature, the Win32 API has several messages:

CB_FINDSTRING Finds a string based on a case-insensitive search of the first
few letters.

CB_FINDSTRINGEXACT Finds the first list box string in a combo box that matches the
string specified.

CB_SELECTSTRING Searches the list of a combo box for an item that begins with the
characters in a specified string. If a matching item is found, it
is selected and copied to the edit control.

Because of the way TComboBox is designed, we will only be using CB_FINDSTRING.
In this example, I load some data from the customers.db table from BCDEMOS to populate the
list, then trap the OnKeyDown and OnChange events.


CB_FINDSTRING takes, as its first parameter (wParam), the index of the item before the start
of the search. Since we are searching the whole list, we set it to -1.
The second parameter is the address of the string to search for.

-------------Code--------------
//cb is the ComboBox
//lastKey is a private member of TForm1


__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
Table1->Open(); //get data from the table
Table1->First();
while (!Table1->Eof)
{
cb->Items->Add(Table1->FieldByName("Contact")->AsString);
Table1->Next();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cbKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
lastKey = Key; //get the last key the user hit in the ComboBox
}
//---------------------------------------------------------------------------

void __fastcall TForm1::cbChange(TObject *Sender)
{
String srch = cb->Text; //text to search for
if ((lastKey == 0x08) || (lastKey == VK_DELETE)) //if they hit backspace or delete,
{ //get out of here

lastKey = 0;
return;
}
lastKey = 0;

int ix = cb->Perform(CB_FINDSTRING,-1,(LPARAM)srch.c_str()); //find the string in the list
if (ix != CB_ERR)
{
cb->ItemIndex = ix; //set the ComboBox text accordingly
cb->SelStart = srch.Length();
cb->SelLength = (cb->Text.Length()-srch.Length()); //select the appropriate text
}
}
//---------------------------------------------------------------------------



qiuafa 2003-04-07
  • 打赏
  • 举报
回复
up
道素 2003-04-07
  • 打赏
  • 举报
回复
还有就是ie的下拉部分可以调节大小怎么设置
道素 2003-04-07
  • 打赏
  • 举报
回复
有没有更好的建议
chifengwatch 2003-04-04
  • 打赏
  • 举报
回复
up
yyfzy 2003-04-04
  • 打赏
  • 举报
回复
另外,TComboBox有自动查找功能,例如你输入ax,如果TComboBox里有以ax开头的内容时会自动查找出匹配的字符串。
yyfzy 2003-04-04
  • 打赏
  • 举报
回复
在该TComboBox的OnExit事件里加上:
int i;
for(i=0;i<ComboBox1->Items->Count;i++)
{
//刚才输入的内容已经保存了,不在保存
if(ComboBox1->Items->Strings[i]==ComboBox1->Text)
break;
}
if(i==ComboBox1->Items->Count||ComboBox1->Items->Count==0)
{
//保存刚才输入的内容
ComboBox1->Items->Insert(0,ComboBox1->Text);
}

另外,你还可以自己完善,例如最多可用保存多少条、程序退出时把已经有的内容写到文件,以便下次程序运行时也能提示等等。
net205 2003-04-04
  • 打赏
  • 举报
回复
学习!
道素 2003-04-04
  • 打赏
  • 举报
回复
继续
Dala 2003-04-04
  • 打赏
  • 举报
回复
继承Combobox制作控件,为每一Item添加Visible属性,修改显示列表的函数,应该不麻烦。
sharkxie 2003-04-04
  • 打赏
  • 举报
回复
关注,
先记录每次输入的内容,然后是不是可以在OnChange中进行搜索?
Jim3 2003-04-04
  • 打赏
  • 举报
回复
关注
道素 2003-04-04
  • 打赏
  • 举报
回复
up
sharkxie 2003-04-04
  • 打赏
  • 举报
回复
那就要你在下拉事件里自己写代码进行搜索,并且覆盖掉他原来的自动查找功能,我想时这样的!
道素 2003-04-04
  • 打赏
  • 举报
回复
yyfzy兄你没大明白我的意思,对不起
我的意思是
数据已经都在
只是每次在下来列表时只显示符合要求的其他不显示

13,824

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧