在一个表中有一个"联系人"字段,请问我要如何才能输入汉字的拼音首字母就能查询到这个联系人呢?比如一个人叫"李三"
我按下键盘的"li"键就将所有的叫李的人名列出来呢?

解决方案 »

  1.   

    给你一段源代码好了。这个是完整的程序。
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        search: TEdit;
        sourcelist: TListBox;
        resultlist: TListBox;
        procedure searchChange(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    function GetPYIndexChar( hzchar:string):char;
    begin
      case WORD(hzchar[1]) shl 8 + WORD(hzchar[2]) of
        $B0A1..$B0C4 : result := 'A';
        $B0C5..$B2C0 : result := 'B';
        $B2C1..$B4ED : result := 'C';
        $B4EE..$B6E9 : result := 'D';
        $B6EA..$B7A1 : result := 'E';
        $B7A2..$B8C0 : result := 'F';
        $B8C1..$B9FD : result := 'G';
        $B9FE..$BBF6 : result := 'H';
        $BBF7..$BFA5 : result := 'J';
        $BFA6..$C0AB : result := 'K';
        $C0AC..$C2E7 : result := 'L';
        $C2E8..$C4C2 : result := 'M';
        $C4C3..$C5B5 : result := 'N';
        $C5B6..$C5BD : result := 'O';
        $C5BE..$C6D9 : result := 'P';
        $C6DA..$C8BA : result := 'Q';
        $C8BB..$C8F5 : result := 'R';
        $C8F6..$CBF9 : result := 'S';
        $CBFA..$CDD9 : result := 'T';
        $CDDA..$CEF3 : result := 'W';
        $CEF4..$D188 : result := 'X';
        $D1B9..$D4D0 : result := 'Y';
        $D4D1..$D7F9 : result := 'Z';
      else
        result := char(0);
      end;
    end;
      function SearchByPYIndexStr
    ( SourceStrs:TStrings;
     PYIndexStr:string):string;
    label NotFound;
    var
      i, j   :integer;
      hzchar :string;
    begin
      for i:=0 to SourceStrs.Count-1 do
        begin
          for j:=1 to Length(PYIndexStr) do
            begin
              hzchar:=SourceStrs[i][2*j-1] 
    + SourceStrs[i][2*j];
              if (PYIndexStr[j]<>'?') and
     (UpperCase(PYIndexStr[j]) <>
     GetPYIndexChar(hzchar)) then goto NotFound;
            end;
          if result='' then result := SourceStrs[i]
          else result := result + Char
    (13) + SourceStrs[i];
    NotFound:
        end;
    end;procedure TForm1.searchChange(Sender: TObject);
    var ResultStr:string;
    begin
      ResultStr:='';
      ResultList.Items.Text := SearchByPYIndexStr
    (Sourcelist.Items, Search.Text);
    end; end.
      

  2.   

    用 lywho(阿勇) 的第一信方法吧,我也是这样用的,给你贴一个函数,汉字首字母->拼音,如 野马->ym,呵呵 function GetEn('野马') = 'ym' 
    function GetEn(CnString: string): string;
      function GetEnChar(cnchar: string): char;
      begin
        case Word(cnchar[1]) shl 8 + Word(cnchar[2]) of
          $B0A1..$B0C4: result := 'a';
          $B0C5..$B2C0: result := 'b';
          $B2C1..$B4ED: result := 'c';
          $B4EE..$B6E9: result := 'd';
          $B6EA..$B7A1: result := 'e';
          $B7A2..$B8C0: result := 'f';
          $B8C1..$B9FD: result := 'g';
          $B9FE..$BBF6: result := 'h';
          $BBF7..$BFA5: result := 'j';
          $BFA6..$C0AB: result := 'k';
          $C0AC..$C2E7: result := 'l';
          $C2E8..$C4C2: result := 'm';
          $C4C3..$C5B5: result := 'n';
          $C5B6..$C5BD: result := 'o';
          $C5BE..$C6D9: result := 'p';
          $C6DA..$C8BA: result := 'q';
          $C8BB..$C8F5: result := 'r';
          $C8F6..$CBF9: result := 's';
          $CBFA..$CDD9: result := 't';
          $CDDA..$CEF3: result := 'w';
          $CEF4..$D188: result := 'x';
          $D1B9..$D4D0: result := 'y';
          $D4D1..$D7F9: result := 'z';
        else
          result := char(0);
        end;
      end;
    var
      i, len: integer;
    begin
      CnString := StringReplace(CnString, ' ', '', [rfReplaceAll]);  //删除空格
      len := Length(CnString) - Length(WideString(CnString));        //汉字个数
      for i := 1 to len do
        result := result + GetEnChar(copy(CnString, 2*i-1, 2));
    end;