开发者

Frame names being assigned automatically

What's so special with Frames that when you create them run-time it gets assigned a name automatically ? This causes a problem when you use them in a loop i.e. :

for i := 0 to 3 do
  TMyFrame.Create(self); //Error on 2nd pass

On first pass, the frame is explicityl named 'MyFrame'. On second pass, it will try to name it again 'MyFrame'开发者_高级运维 which obivously will cause an exception as there is already a component with such name.

The other components or forms are happy to be created without me assigning them a name. What gives ?

This is on Delphi 2006 btw.

Cheers


If you are using TFrame to create your Frame, then it wont raise that error.

i.e., If your code looks like this, you wont have any problems

for i := 0 to 3 do 
  Frame1 := TFrame.Create(self);

because here the Name property is unassigned. If you want you can check by using the statement ShowMessage(Frame1.Name);

But if you use TMyFrame i.e., the frame which you derived from TFrame, then it will giving the problem.

I think the reason is, Delphi was explicitly assigning the same Name every time you create the TMyName.


you're trying to create the same frame 4 times, each time you create the frame it will have the same name, one way to avoid is

...
var
  i: Integer;
  lFrame: TFrame;
begin
  for i := 0 to 3 do begin
    lFrame := TFrame.Create(Self);
    // assign a unique name to the frame
    lFrame.Name := Format('MyFrame%d', [i]);
    // set the parent, align, etc...
  end;
end;


Co-incidentally I just bumped into this myself (again).

If you take the name out of your frame component then Delphi moans that "Root Component must have a name" - I guess it because somewhere Delphi is calling RegisterClass(RootComponentName) and then to create it (or a descendent) it's calling Findclass(RootComponentName) or similar. As you can do this yourself to create components that you don't actually know the classname of a design time, why would Delphi not do the same?

Whereas with a TButton etc. it is already a registered Class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜