本文整理自网络,侵删。
以下在WIN764 +DELPHI XE 10.3测试通过;
运行时界面:

代码如下:
unit Unit1;
interface
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; Memo2: TMemo; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; Button6: TButton; procedure Button1Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.dfm} uses Generics.Collections;
procedure TForm1.Button1Click(Sender: TObject);Var MyDict : TObjectDictionary<String, TStringList>; Sl : TStringList;
begin ReportMemoryLeaksOnShutdown:=True; try
MyDict := TObjectDictionary<String, TStringList>.Create([doOwnsValues]); try memo1.Clear ; memo2.clear; Sl:=TStringList.Create;
Sl.Add('Foo 1'); Sl.Add('Foo 2'); Sl.Add('Foo 3');
MyDict.Add('1',Sl);
MyDict.Add('2',TStringList.Create); MyDict.Items['2'].Add('Line 1'); MyDict.Items['2'].Add('Line 2'); MyDict.Items['2'].Add('Line 3');
//finally show the stored data memo1.Lines .add(MyDict.Items['1'].Text); memo2.Lines.add(MyDict.Items['2'].Text); finally //only must free the dictionary and don't need to worry for free the TStringList assignated to the dictionary MyDict.Free; end; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end;end;
procedure TForm1.Button2Click(Sender: TObject); Var bt :TObjectDictionary<String, TButton >; PairEnum:TObjectDictionary<String, TButton >.TPairEnumerator;begin//对象字典2 memo1.Clear ; memo2.clear; ReportMemoryLeaksOnShutdown:=true; try bt :=TObjectDictionary<String, TButton >.Create([doOwnsValues]); try bt.Add('a',TBUTTON(button2)); bt.Add('B',TBUTTON(button3)); bt.Add('c',TBUTTON(button4)); bt.Add('D',TBUTTON(button5)); memo2.Lines .add(bt.Items['a'].Name); memo2.Lines .add(bt.Items['B'].Name); memo2.Lines .add(bt.Items['c'].Name); memo2.Lines .add(bt.Items['D'].Name); memo2.Lines .add('---------------------------'); PairEnum :=bt.GetEnumerator;
while PairEnum.MoveNext do begin memo2.Lines.Add(PairEnum.Current.Key+'=' +TBUTTON(PairEnum.Current.Value).Name );
end; finally //only must free the dictionary and don't need to worry for free the TStringList assignated to the dictionary PairEnum.CleanupInstance; PairEnum.Free; bt.CleanupInstance; bt.Free end; except on E: Exception do showmessage(E.ClassName+': '+ E.Message); end;
end;
procedure TForm1.Button3Click(Sender: TObject);Var
bt :TObjectDictionary<String, TButton >;begin //对象字典 memo1.Clear ; memo2.clear; ReportMemoryLeaksOnShutdown:=true; try bt :=TObjectDictionary<String, TButton >.Create([doOwnsValues]); try bt.Add('a',TBUTTON(button2)); bt.Add('B',TBUTTON(button3)); bt.Add('c',TBUTTON(button4)); memo1.Lines .add(bt.Items['a'].Name); memo1.Lines .add(bt.Items['B'].Name); memo1.Lines .add(bt.Items['c'].Name); finally //only must free the dictionary and don't need to worry for free the TStringList assignated to the dictionary
bt.CleanupInstance; bt.Free end; except on E: Exception do showmessage(E.ClassName+': '+ E.Message); end;
end;
end.
――――――――――――――――
原文链接:https://blog.csdn.net/qq_25439957/article/details/88624869
相关阅读 >>
Delphi xe8安卓下汉字转gb2312 urlencode代码
Delphi �c 如何将多个文件扩展名传递给tdirectory.getfiles?
更多相关阅读请进入《Delphi》频道 >>