【发布时间】:2018-03-22 17:14:43
【问题描述】:
我有一个应用程序,其中包括“主”表单以及其他四种表单(formA、formB、formC、formD)。四种形式中的每一种都有一个唯一的class function execute()。
现在,在 formD 中,我放置了一个编辑框和一个按钮。在按钮的OnClick事件上,根据我在编辑框中传入的表单名称,我想运行相应的类函数。
我试图通过创建一个TDictionary 来实现这个任务,我在其中添加了 3 对值,但它不起作用。具体来说,我做了以下事情:
unit FormDU;
interface
type
TFormD = class(TForm)
Edit1: TEdit;
fcShapeBtn1: TfcShapeBtn;
.............
.............
public
class function execute:boolean;
..........
..........
end;
var
FormD: TFormD;
MyList:TDictionary<string,TForm>;
implementation
class function TFormD.execute:boolean;
begin
FormD:= TFormD.Create(nil);
MyList:= TDictionary<string,TForm>.create;
MyList.Add('FormA',TFormA);
MyList.Add('FormB',TFormB);
MyList.Add('FormC',TFormC);
FormD.showmodal;
end;
procedure TFormD.fcShapeBtn1Click(Sender: TObject);
begin
// Here I check whether the text in Edit1 box has the value of one of the
// keys that are included in the MyList dictionary and if yes I want to
// trigger the class function execute of the appropriate form...
if MyList.ContainsKey(Edit1.text) then // suppose that text= formA
MyList.Items[Edit1.text].execute // which doesn't work....
// I thought that the 'Items' method of the dictionary would return back
// to me the appropriate form type - which is connected to the specific
// key - and thus I could call each form's class function execute()
end;
我不知道如何解决这个问题。
【问题讨论】: