码迷,mamicode.com
首页 > Windows程序 > 详细

delphi2010泛型练习

时间:2016-03-08 19:18:40      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
{ 本单元使用 TDictionary 练习测试 }
TMyGrid = class(TStringgrid);{能使用整行删除}
uses
  StrUtils,  { 使用函数必须在此 添加 strUtils }
  Generics.Collections;   {Delphi 泛型容器单元}
var
  Dict: TDictionary<string,string>;  {定义一个泛型 TDictionary 类, 指定有 string、string 构成}
  num:byte;  r:word;
  key,item: string;
{添加}
procedure TForm1.Button1Click(Sender: TObject);
begin
  if r>=999 then   exit;
  key :=edit1.Text;  item := Edit2.Text;
  if item=‘‘ then item:=‘Null‘;
  if not Dict.ContainsKey(key) then
    begin
      Dict.Add(key,item);
      grid.Cells[1,r]:=key;      grid.Cells[2,r]:=item;
      grid.rowcount:=grid.RowCount+1;      grid.Row:=r;
      r:=r+1;
      num:= 3-length(inttostr(r));
      edit1.Text:=DupeString(‘0‘,num) + inttostr(r);
      edit2.Text:=‘‘;      edit2.SetFocus;
    end
  else
    showmessage(‘该会员代号已存在,不能重复添加。‘);
end;
 
procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
    button1.Click;
end;
{ 删除 }
procedure TForm1.Button2Click(Sender: TObject);
begin
  key:=grid.Cells[1,grid.row];
  if key=‘‘ then  exit;
  if InputQuery(‘输入‘,‘请输入key值‘,key) then
    if key<>‘‘ then //如果输入不为空则
      if dict.ContainsKey(key) then
      begin
        dict.Remove(key);
        grid.Rows[strtoint(key)].Clear;
        TMyGrid(grid).DeleteRow(strtoint(key));{ 整行删除}
      end
      else
        showmessage(‘不存在‘);
end;
{ 取值 }
procedure TForm1.Button3Click(Sender: TObject);
begin
  if InputQuery(‘输入‘,‘请输入key值‘,key) then
    if key<>‘‘ then //如果输入不为空则
      if dict.ContainsKey(key) then
        showmessage(dict.Items[key])
      else
        showmessage(‘不存在‘);
end;
{ 清空 }
procedure TForm1.Button4Click(Sender: TObject);
begin
  Dict.Clear; { Grid同步显示 }
  for r:=1 to grid.RowCount-1  do
     grid.Rows[r].Clear;
  grid.RowCount:=2;
end;
{ 计算重复次数 }
procedure TForm1.Button5Click(Sender: TObject);
var
  dict2:TDictionary<string,byte>;
  n:byte;
begin
  dict2:=TDictionary<string,byte>.create;
  for r:=1 to grid.RowCount-2 do
    if dict2.TryGetValue(grid.Cells[2,r], n) then
      dict2[grid.Cells[2,r]]:= n+1
    else
      dict2.Add(grid.Cells[2,r], 1);
  meo.Clear;
  for key in dict2.Keys  do
    meo.lines.add(key+‘,出现次数:‘+inttostr(dict2.Items[key]));
end;
 
procedure TForm1.FormActivate(Sender: TObject);
begin
  edit2.SetFocus;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  Dict := TDictionary<string,string>.Create;
  Button1.Caption := ‘添加‘;
  Button2.Caption := ‘删除‘;
  Button3.Caption := ‘取值‘;
  Button4.Caption := ‘清空‘;
  Edit1.text:=‘001‘;
  Edit2.Text:=‘绿杨荫里‘;
  Edit1.NumbersOnly := True;
  grid.ColWidths[0]:=15;
  grid.ColWidths[1]:=80;
  grid.ColWidths[2]:=130;
  grid.Cells[1,0]:=‘Key‘;
  grid.Cells[2,0]:=‘value‘;
  r:=1;
end;
{释放}
procedure TForm1.FormDestroy(Sender: TObject);
begin
  Dict.Free;
end;
技术分享
 

delphi2010泛型练习

标签:

原文地址:http://www.cnblogs.com/lvyangyinli/p/5255174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!