delphi2010泛型练习


{ 本单元使用 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;


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值