Recent

Author Topic: Setting the name of a control created at runtime  (Read 4197 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
Setting the name of a control created at runtime
« on: June 24, 2014, 04:54:40 pm »

When I create a control at run time and set the parent, an error is raised if the parent contains a control with the same name. Is there an option to  code to give a control a sequential name similar how the IDE does it at design time?

Is it possible for controls to have the same name so long as they have different parents?
Lazarus 3.0/FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 12910
Re: Setting the name of a control created at runtime
« Reply #1 on: June 24, 2014, 05:15:28 pm »
I normally leave the name of run-time created controls empty. If I need them I call them directly by their variable name instead of seeking them based on their "name" property.
« Last Edit: June 24, 2014, 05:57:49 pm by wp »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Setting the name of a control created at runtime
« Reply #2 on: June 24, 2014, 05:18:47 pm »
no parents have nothing to do with the name use only owners do. Why do you need to name a runtime created control in the first place the name is used by the streaming mechanism and that is the reason it has to be unique. Leaving the name empty should allow you to have multiple of those controls with out a problem. I any case there is the file ideCommands of ideintf package which has the TIDECommandScopes class with the method CreateUniqueName. Take a look on how it is implemented I guess you would need to duplicate it in your own code.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
Re: Setting the name of a control created at runtime
« Reply #3 on: June 25, 2014, 04:09:26 pm »
The object in question is actually a frame. I think Frames are different controls and I remember reading somewhere that they need customized constructors in some cases.

Do you know of some documentation which covers this topic and how overriding the constructor can help in the case?

no parents have nothing to do with the name use only owners do. Why do you need to name a runtime created control in the first place the name is used by the streaming mechanism and that is the reason it has to be unique. Leaving the name empty should allow you to have multiple of those controls with out a problem. I any case there is the file ideCommands of ideintf package which has the TIDECommandScopes class with the method CreateUniqueName. Take a look on how it is implemented I guess you would need to duplicate it in your own code.
Lazarus 3.0/FPC 3.2.2

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Setting the name of a control created at runtime
« Reply #4 on: June 25, 2014, 04:59:45 pm »
The object in question is actually a frame. I think Frames are different controls and I remember reading somewhere that they need customized constructors in some cases.

Do you know of some documentation which covers this topic and how overriding the constructor can help in the case?

No sorry I don't remember any frame specific documentation and I do not understand what the constructor could do in this case that a simple procedure can't accomplish. In any case just leave it empty I do it all the time in my dynamically created frames with no problems so far.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

jdlinke

  • Jr. Member
  • **
  • Posts: 62
  • Just old me
Re: Setting the name of a control created at runtime
« Reply #5 on: June 25, 2014, 09:39:10 pm »
Much like others have posted here, this is how I have approached this problem. I'm using an array of frames, but you can do the same thing when making several independent frames at runtime.

Code: [Select]
unit formUnitMain;


{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  types, ExtCtrls, Buttons, ComCtrls, unitTypeTile;

type

  { TformMain }

  TformMain = class(TForm)
    ScrollBox1: TScrollBox;
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    FrameTypeTile : array of TframeTypeTile; // My frames
  end;

var
  formMain: TformMain;

implementation

{$R *.lfm}


{ TformMain }

procedure TformMain.FormCreate(Sender: TObject);
var
  i : Integer;
  fileList : TStringList;
begin

  // I do some processing here to get a list of files (fileList) for my particular application

  SetLength(FrameTypeTile, 1); // FrameTypeTile is the frame I designed

  for i := fileList.Count-1 downto 0 do // Use downto so my frames are in the right order when using align:=alTop
  begin

    SetLength(FrameTypeTile, Length(FrameTypeTile)+1);
   
    FrameTypeTile[i] := TframeTypeTile.Create(formMain); // Who owns the frame(s)
    FrameTypeTile[i].Name := ''; // Blank works just fine!
    FrameTypeTile[i].Parent := ScrollBox1; // Where the frame will be placed
    FrameTypeTile[i].Align := alTop;

  end;

end;

end.

Hope it helps.
Lazarus 1.2.4 32-bit version on Windows 8.1 64-bit, Windows 7 64-bit, and Windows XP 32-bit

Currently developing TimberLog and the VirtualDBScroll Component Package

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
Re: Setting the name of a control created at runtime
« Reply #6 on: June 26, 2014, 12:53:39 pm »
The object in question is actually a frame. I think Frames are different controls and I remember reading somewhere that they need customized constructors in some cases.

Do you know of some documentation which covers this topic and how overriding the constructor can help in the case?

It turned out to be much easier than I suspected. It was a simple matter of overriding the constructor and setting the Name=''

Code: [Select]
constructor TFrame01.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Name := '';
end;

Lazarus 3.0/FPC 3.2.2

jdlinke

  • Jr. Member
  • **
  • Posts: 62
  • Just old me
Re: Setting the name of a control created at runtime
« Reply #7 on: June 26, 2014, 03:07:17 pm »
Quote
It was a simple matter of overriding the constructor and setting the Name=''

Nice. That's a bit more elegant than setting the name in the main program as each instance of the frame is created as I was doing
Lazarus 1.2.4 32-bit version on Windows 8.1 64-bit, Windows 7 64-bit, and Windows XP 32-bit

Currently developing TimberLog and the VirtualDBScroll Component Package

 

TinyPortal © 2005-2018