Link to home
Start Free TrialLog in
Avatar of the_modder
the_modderFlag for United States of America

asked on

Component Exists?

I am creating compoents in run time. I would like to know how I can know if a component was created.

Thanks,
David B
Avatar of kretzschmar
kretzschmar
Flag of Germany image

var
  AComponent : TComponent;
begin
  AComponent := FindComponent('AComponentName');
  if assigned(AComponent) then //it exists
  ...


meikl ;-)
Avatar of cloaking_device
cloaking_device

if you are actually creating components in runtime, you may do it this way:

(you really don't have to use colordialog, this applies with anything)

var AComponent: TColorDialog;
begin

  AComponent := TColorDialog.Create(nil);
  if(Assigned(AComponent)) then
    AComponent.Execute;

  AComponent.Free;

end;
Avatar of the_modder

ASKER

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  button3 : tbutton;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
var
 AComponent : TComponent;
begin
 AComponent := FindComponent('button3');
 if assigned(AComponent) then //it exists
 showmessage('exists')
 else
 showmessage('does not exists');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
button3 := tbutton.Create(self);
button3.Parent := self;
button3.Visible := true;
end;

end.


Does not work. Always says non existant.
var
 AComponent : TComponent;
begin
 AComponent := FindComponent('AComponentName');
 if assigned(AComponent) then //it exists

works, but only with buttons that were placed in the form before.
ASKER CERTIFIED SOLUTION
Avatar of cloaking_device
cloaking_device

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if(Assigned(button3)) then
showmessage('sup');

works!

Thanks!
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button4: TButton;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  button3: tbutton;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
begin
button3 := tbutton.Create(self);
button3.Parent := self;
button3.Visible := true;
//button3 := nil;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 AComponent : TComponent;
begin
if(Assigned(button3)) then
showmessage(works);

end;

procedure TForm1.Button4Click(Sender: TObject);
begin
button3.destroy;
end;

end.


It does not work when I create the button and then destroy it. It will still say that it works!
^
Bump. I will give extra points to whoever solves this issue.
...
var
 Form1: TForm1;
 //button3: tbutton; //not needed

...
procedure TForm1.Button2Click(Sender: TObject);
begin
  if not assigned(findcomponent('button3')) then
    with tbutton.Create(self) do
    begin
      Parent := self;
      visible := true;
      name := 'button3';
    end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  if Assigned(findcomponent('button3')) then
    showmessage(works);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  if Assigned(findcomponent('button3')) then
    findcomponent('button3').free;
end;

end.

just not tested

meikl ;-)
btw.
>It does not work when I create the button and then destroy it. It will still say that it works!
thats just luck, that the memory is not overwritten, usual you should get an access-violation
heh.... 2 tips when using destroy

1. usually you do not want to destroy any component that is owned by the Form... because it is very very costly. If you are going to create and destroy a component by yourself, you're better off setting owner to nil (TButton.Create(nil)). This is because when creating or destroying a component owned by another, it must notify every component on the form that yours was destroyed. Memory costly. The more components you have on the form, the worse.

But this is nitpicky =)

2. Also, you generally want to set your pointer to NIL after you destroy/free something. If you don't do this, and you try to access the pointer, you will almost definetly get access violations, because you are now accessing someone else's memory space!!

If you set your pointer to NIL, "Assigned" function will work again.

Remember, pointers are just numbers... to addresses. Your button3 variable is just a number, the address of your created memory space. Even if you destroy the button, if you don't clear the pointer it still points to the same address. Nothing we can do about this.

However, i'll look around for another solution just in case it doesn't get set to nil.

Please look for how to detect if it was destroyed. I tried settin the owner to nil and it still does not work :(
i meant:

procedure TForm1.Button4Click(Sender: TObject);
begin
button3.Free;
button3 := nil;
end;
Great! Thanks cloadking_device!

Visit this: https://www.experts-exchange.com/questions/20738501/Points-for-cloaking-device.html for your points.