用C++ Builder 实现类似ie地址栏的ComboBox (转)

用C++ Builder 实现类似ie地址栏的ComboBox (转)[@more@]

大家一定对ie浏览器的地址栏很熟悉,几乎没有没用过的人,当你输入一串字符时,下拉列表会列出所有历史记录中存放的地址前几位字符和输入字符相符的条目。

我们在设计程序也需要这种技术以方便用户输入,它是怎么实现的呢?

下面我就说说我用CBuilder实现这种效果的方法:

首先新建一个应用程序,在窗体上ComboBox,名称设为:ComboBox1

在头文件中声明一个stringlist

TStringList *MyDropDownList;

在应用程序初始化部门将所有列表数据存入MyDropDownList;

在ComboBox1的KeyPress中加入以下代码:

nt i, iInputLength, iSelStartRestore, iSelLengthRestore;
AnsiString strInput;
TStringList *TempList;

strInput= ComboBox1->Text;


  if (Key == VK_ESCAPE)
  {
  Key = 0x0; // No more beeping after pressing Escape.
  }

  if (Key == VK_RETURN)
  {
  Key = 0x0;
  if (ComboBox1->Items->IndexOf(strInput) == -1) ComboBox1->Items->Add(strInput);
  ComboBox1->DroppedDown = False;

  ComboBox1->SelStart = ComboBox1->Text.Length();

  }
  else
  {
  iSelStartRestore = ComboBox1->SelStart;
  iSelLengthRestore = ComboBox1->SelLength;
  if (Key == VK_BACK)
  {
  // Handle backspace:
  if ((ComboBox1->SelLength == 0) && (ComboBox1->SelStart > 0))
  {
  ComboBox1->SelStart = ComboBox1->SelStart - 1;
  ComboBox1->SelLength = ComboBox1->SelLength + 1;
  }
  }
  strInput.Delete(ComboBox1->SelStart + 1, ComboBox1->SelLength);

  if (Key != VK_BACK)
  {
  strInput.Insert(Key, ComboBox1->SelStart + 1);
  }
  iInputLength = strInput.Length();
  ComboBox1->Items->Clear();
  if (iInputLength > 0)
  {

  TempList = new TStringList;
  try
  {
  for ( i= 0; iCount - 1;i++)
  {
  if ((MyDropDownList->Strings[i].SubString(1, iInputLength)).UpperCase() ==
  strInput.UpperCase())
  TempList->Add(MyDropDownList->Strings[i]);

  }
  if (TempList->Count > 0)
  {
  for (i = 0 ;i<7;i++) ComboBox1->Items->Add("");
  ComboBox1->DropDownCount = 8;
  ComboBox1->DroppedDown = True;
  ComboBox1->Items->Clear();
  ComboBox1->Items = TempList;
  }
  else ComboBox1->DroppedDown = False;
  }

  __finally
  {
  TempList->Free();
  }
  }
  else

  ComboBox1->DroppedDown = False;
  // Restore the position of the carrot and the selected text:
  ComboBox1->SelStart = iSelStartRestore;
  ComboBox1->SelLength= iSelLengthRestore;

}

在C++ Builder win2000下实现,其他应该也可以。

 

 

 

 

 


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-998864/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-998864/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值