---- 1.进入Delphi创建一个新工程:Project1<br><br>---- 2.在Form1上创建以下控件并填写属性:<br><br>控件类型 属性名称 属性值<br>Edit Name Search<br>ListBox Name SourceList<br>Items 输入一些字符串,如姓名等,用于提供检索数据<br>ListBox Name ResultList<br>---- 3.键入以下两个函数<br><br>// 获取指定汉字的拼音索引字母,如:“汉”的索引字母是“H”<br>function GetPYIndexChar( hzchar:string):char;<br>begin<br> case WORD(hzchar[1]) shl 8 + WORD(hzchar[2]) of<br> $B0A1..$B0C4 : result := 'A';<br> $B0C5..$B2C0 : result := 'B';<br> $B2C1..$B4ED : result := 'C';<br> $B4EE..$B6E9 : result := 'D';<br> $B6EA..$B7A1 : result := 'E';<br> $B7A2..$B8C0 : result := 'F';<br> $B8C1..$B9FD : result := 'G';<br> $B9FE..$BBF6 : result := 'H';<br> $BBF7..$BFA5 : result := 'J';<br> $BFA6..$C0AB : result := 'K';<br> $C0AC..$C2E7 : result := 'L';<br> $C2E8..$C4C2 : result := 'M';<br> $C4C3..$C5B5 : result := 'N';<br> $C5B6..$C5BD : result := 'O';<br> $C5BE..$C6D9 : result := 'P';<br> $C6DA..$C8BA : result := 'Q';<br> $C8BB..$C8F5 : result := 'R';<br> $C8F6..$CBF9 : result := 'S';<br> $CBFA..$CDD9 : result := 'T';<br> $CDDA..$CEF3 : result := 'W';<br> $CEF4..$D188 : result := 'X';<br> $D1B9..$D4D0 : result := 'Y';<br> $D4D1..$D7F9 : result := 'Z';<br> else<br> result := char(0);<br> end;<br>end;<br><br>// 在指定的字符串列表SourceStrs中检索符合拼音索引字符串<br>PYIndexStr的所有字符串,并返回。<br>function SearchByPYIndexStr<br>( SourceStrs:TStrings;<br>PYIndexStr:string):string;<br>label NotFound;<br>var<br> i, j :integer;<br> hzchar :string;<br>begin<br> for i:=0 to SourceStrs.Count-1 do<br> begin<br> for j:=1 to Length(PYIndexStr) do<br> begin<br> hzchar:=SourceStrs[2*j-1] <br>+ SourceStrs[2*j];<br> if (PYIndexStr[j]<>'?') and<br>(UpperCase(PYIndexStr[j]) <><br>GetPYIndexChar(hzchar)) then goto NotFound;<br> end;<br> if result='' then result := SourceStrs<br> else result := result + Char<br>(13) + SourceStrs;<br>NotFound:<br> end;<br>end;<br><br>4.增加编辑框Search的OnChange事件:<br>procedure TForm1.SearchChange(Sender: TObject);<br>var ResultStr:string;<br>begin<br> ResultStr:='';<br> ResultList.Items.Text := SearchByPYIndexStr<br>(Sourcelist.Items, Search.Text);<br>end;