远程数据库中有一股票代码表,格式如下: id      Name   NamePY
-----------------------
000001 深发展A SFZA
000002 万科A WKA
000004 ST国农 STGN
000005 ST星源 STXY
000006 深振业A SZYA
000007 ST达声 STDS
000008 *ST宝投 *STBT
...
...我将这个表读进一个ComboBox中,ComboBox下拉框中的内容就是"000001 深发展A SFZA"的格式,若我在ComboBox中输入汉语拼音的时候,如输入"S",则ComboBox的下拉框中出现所有"NamePY"中包含这个字母的所有的股票名称

解决方案 »

  1.   

    没事。。写了一个,不过效率不高,呵呵
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, DB, ADODB;type
      TForm1 = class(TForm)
        ADOConnection1: TADOConnection;
        ADOQuery1: TADOQuery;
        ComboBox1: TComboBox;
        procedure FormCreate(Sender: TObject);
        procedure Comboboxinit(sender:Tobject);
        procedure ComboBox1Change(Sender: TObject);
        procedure ComboBox1Select(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure Tform1.formcreate(sender:tobject);
    begin
           comboboxinit(sender);
    end;procedure TForm1.comboboxinit(Sender: TObject);  //初始化combobox内容
    var
        idtmp,nametmp,namepytmp:string;
        fullinfo:string;
    begin
         combobox1.Clear;
          with adoquery1 do
          begin
               close;
               sql.clear;
               sql.add('select * from gupiao order by id');
               open;
          end;      if adoquery1.RecordCount>0 then
          begin
            while not adoquery1.eof do  //添加表的全部信息到combobox
            begin
              idtmp:=adoquery1.fieldbyname('id').Value;
              nametmp:=adoquery1.fieldbyname('name').Value;
              namepytmp:=adoquery1.fieldbyname('namepy').Value;
              fullinfo:=idtmp+'   '+nametmp+'   '+namepytmp;
              combobox1.Items.add(fullinfo);
              adoquery1.Next;
            end;
          end;
    end;procedure TForm1.ComboBox1Change(Sender: TObject);
    var
        id,name,py:string;
        info:string;
    begin
       if trim(combobox1.Text)='' then
         comboboxinit(sender)
       else
       begin
           combobox1.items.Clear;  //清空列表
           with adoquery1 do
           begin
               close;
               sql.clear;
               sql.add('select * from gupiao where namepy like :sqlstr order by id');
               parameters.ParamByName('sqlstr').Value:='%'+combobox1.Text+'%'; 
               open;
          end;      if adoquery1.RecordCount>0 then
          begin
            while not adoquery1.eof do
            begin
              id:=adoquery1.fieldbyname('id').Value;
              name:=adoquery1.fieldbyname('name').Value;
              py:=adoquery1.fieldbyname('namepy').Value;
              info:=id+'   '+name+'   '+py;
              combobox1.Items.add(info);
              adoquery1.Next;
            end;
          end;
          combobox1.AutoDropDown:=true;
       end;
    end;procedure TForm1.ComboBox1Select(Sender: TObject);
    begin
        combobox1.Text:=combobox1.SelText;
    end;end.
    两次查询了,只能说实现效果了。另外,可以考虑把表的信息保存到Tstrings种。。
    然后再使用pos 函数,循环依次比较..关注其他的做法
      

  2.   

    用 pos 函数 进行查询就可以了,
      

  3.   

    不需要在 combobox 每变化就执行查询,查询只要做一次就可以,把这些数据存放在DataSet中即在内存中。
    然后在 combobox.text 变化时,用 DataSet.Locate 方法进行过滤,过滤条件用loPartialKey,这样可提高效率。否则当所查询的表中数据量很大时,效率很低,但如果数据量很大,会很耗内存就是。