Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2009-04

Invalid component names

19 messages · 9 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Hans-Peter Diettrich

On Win32 every component (at least control) gets an unique Name on creation. On Linux a dynamically created control has an empty name.

Is this a Lazarus or FPC problem?

I could not trace down to the point where the name is assigned in win32, because Lazarus denies setting an breakpoint on TComponent.SetName :-(

DoDi

Reply by email

Gabor Boros
to Hans-Peter Diettrich: On Win32 every component (at least control) gets an unique Name on creation. On Linux a…

Hi,

Dynamically created components have empty name after creation on Win32 too. This thing is true for Delphi too. This is normal I think.

An example:

var
   MyLabel:TLabel;
begin
   MyLabel:=TLabel.Create(Self);
   Button1.Caption:=MyLabel.Name;
end;

Button1's caption is empty with Lazarus on Win32 and with Delphi too.

Gabor

Hans-Peter Diettrich ?rta:

Show quoted text 15 lines
On Win32 every component (at least control) gets an unique Name on creation. On Linux a dynamically created control has an empty name. Is this a Lazarus or FPC problem? I could not trace down to the point where the name is assigned in win32, because Lazarus denies setting an breakpoint on TComponent.SetName :-( DoDi _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Vincent Snijders
to Gabor Boros
Gabor Boros wrote 17 lines
Hi, Dynamically created components have empty name after creation on Win32 too. This thing is true for Delphi too. This is normal I think. An example: var MyLabel:TLabel; begin MyLabel:=TLabel.Create(Self); Button1.Caption:=MyLabel.Name; end; Button1's caption is empty with Lazarus on Win32 and with Delphi too.

I thinkk so too, that is why I asked for the example.

Show quoted text 8 lines
Hans-Peter Diettrich ?rta: > On Win32 every component (at least control) gets an unique Name on > creation. On Linux a dynamically created control has an empty name. > > Is this a Lazarus or FPC problem? > > I could not trace down to the point where the name is assigned in win32, > because Lazarus denies setting an breakpoint on TComponent.SetName :-(

Vincent

Reply by email

Hans-Peter Diettrich
to Gabor Boros: Dynamically created components have empty name after creation on Win32 too. This thing is…
Dynamically created components have empty name after creation on Win32 too. This thing is true for Delphi too. This is normal I think.

Right, I was misleaded by different settings in my various test projects. Only forms get an unique name, and this works in Lazarus as well as in Delphi.

So the remaining question is: How to assign unique names to dynamically created components? Does there exist an immediately usable method or function?

Empty names seem to be harmless, but every attempt to assign an non-empty and already used name to a component results in an exception.

What's the scope (list of names...), within which every component must have an unique name?

DoDi

Reply by email

Mattias Gaertner
to Hans-Peter Diettrich
Show quoted text 20 lines
Gabor Boros schrieb: > Dynamically created components have empty name after creation on > Win32 too. This thing is true for Delphi too. This is normal I > think. Right, I was misleaded by different settings in my various test projects. Only forms get an unique name, and this works in Lazarus as well as in Delphi. So the remaining question is: How to assign unique names to dynamically created components? Does there exist an immediately usable method or function? Empty names seem to be harmless, but every attempt to assign an non-empty and already used name to a component results in an exception. What's the scope (list of names...), within which every component must have an unique name?

Have you ?

Mattias

Reply by email

Hans-Peter Diettrich
to Gabor Boros
Show quoted text 9 lines
I always use the variable's name to component name. var MyLabel15:TLabel; begin MyLabel15:=TLabel.Create(Self); MyLabel15.Name:='MyLabel15'; end;

Exactly this code will cause an exception, when the procedure is called more than once - as I need it to create multiple floating controls.

DoDi

Reply by email

dmitry boyarintsev
to Gabor Boros: I always use the variable's name to component name. var MyLabel15:TLabel; begin…

to be sure about name is unique name at the run time there're at least two ways.

1st) global counter

var
  GlobalCounter : integer = 0;
function GetUniqueName(ClassName: String = ''): string;
begin
  if PrefixName = '' then Result := 'dynamicComponent'+IntToStr(GlobalCounter)
  else Result := Copy(ClassName, 2, length(ClassName)-1) +
IntToStr(GlobalCounter);
  // need to care about thread safety here.
  inc(GlobalCounter);
end;

2nd) use GUIDs.

function GetUniqueName(ClassName: String = ''): string;
var
  g : TGUID;
begin
  SysUtils.CreateGUID(g);
  Result := ClassName + GUIDToString(g);
end;

...

  AComponent := TSomeType.Create( owner );
  AComponent.Name := GetUniqueName(AComponent.ClassName);

thanks,
dmitry

Reply by email

Hans-Peter Diettrich
to Mattias Gaertner: On Sun, 26 Apr 2009 19:54:58 +0200 Have you ? Mattias
Show quoted text 12 lines
> So the remaining question is: How to assign unique names to > dynamically created components? Does there exist an immediately > usable method or function? > > Empty names seem to be harmless, but every attempt to assign an > non-empty and already used name to a component results in an > exception. > > What's the scope (list of names...), within which every component > must have an unique name? Have you ?

Thanks, that answers the latter question.

Nonetheless there should exist a method, at least in the form designer, for creating unique names. Where is it?

DoDi

Reply by email

Gerard N/A
to Hans-Peter Diettrich
Hans-Peter Diettrich wrote
Nonetheless there should exist a method, at least in the form designer, for creating unique names. Where is it?

Have a look at function TCustomFormEditor.CreateUniqueComponentName in ide\customformeditor.pp

Regards,

Gerard.

Reply by email

Benito van der Zander
> So the remaining question is: How to assign unique names to > dynamically created components? Does there exist an immediately > usable method or function? >
For wincontrols you could use this:
control.name:='handle_is_'+inttostr(control.handle);

Handle is system-wide unique (at least on Win32)

However, some properties can change the handle imho, than you should either set the new name or hope the handle will not be used again.

Or you could use this: (name depending on the current time and a random number if several controls are created in one milli-second) component.name:='created_at_'+inttostr(trunc(date))+'_'+inttostr(trunc(time*24*60*60*3600))+'_'+inttostr(random(100000));

Benito

Reply by email

Hans-Peter Diettrich
to Vincent Snijders: Why do you want to give the controls names? Vincent
> Exactly this code will cause an exception, when the procedure is called > more than once - as I need it to create multiple floating controls. Why do you want to give the controls names?

Names are convenient to identify floating windows (label title bars) or docked controls (dock header label), and are required for the de/serialization of docking layouts.

But you are right in so far, as dynamically created controls are of little use in the recreation of a docking layout. In this case the names should be hardwired somehow, so that the appropriate controls can be created from the stored layout. I'll have to re-think this issue in the implementation of multiple editor windows and the related forms. Then e.g. the code explorer windows etc. must be linked to the edit control in their dock site, regardless of their name in creation order.

DoDi

Reply by email

Gabor Boros
to Hans-Peter Diettrich: Exactly this code will cause an exception, when the procedure is called more than once -…

Yes, I know. But I just want to show an example.

Gabor

Hans-Peter Diettrich ?rta:

Show quoted text 16 lines
Gabor Boros schrieb: > I always use the variable's name to component name. > > var > MyLabel15:TLabel; > > begin > MyLabel15:=TLabel.Create(Self); > MyLabel15.Name:='MyLabel15'; > end; Exactly this code will cause an exception, when the procedure is called more than once - as I need it to create multiple floating controls. DoDi

Reply by email

Paul Ishenin
to Mattias Gärtner: An embedded and charset-unspecified text was scrubbed... Name: not available Url:…
Mattias G?rtner wrote 8 lines
Welcome to the complex world of layout restoration. ;) Just an idea: The anchor docking manager has one component per docked control. It allows to set a unique name and will use by default the component name. The manager will resolve conflicts by appending a number. The user can set the name via the designer and a translated name for i18n.

What is the need to have a name at all? Can layout be streamed using own format like xml or ini file?

Best regards,
Paul Ishenin.

Reply by email