the_modder
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
Thanks,
David B
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;
(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;
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.
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
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
begin
button3 := tbutton.Create(self);
button3.Parent := self;
button3.Visible := true;
end;
end.
Does not work. Always says non existant.
ASKER
var
AComponent : TComponent;
begin
AComponent := FindComponent('AComponentN ame');
if assigned(AComponent) then //it exists
works, but only with buttons that were placed in the form before.
AComponent : TComponent;
begin
AComponent := FindComponent('AComponentN
if assigned(AComponent) then //it exists
works, but only with buttons that were placed in the form before.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
if(Assigned(button3)) then
showmessage('sup');
works!
Thanks!
showmessage('sup');
works!
Thanks!
ASKER
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!
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
begin
button3 := tbutton.Create(self);
button3.Parent := self;
button3.Visible := true;
//button3 := nil;
end;
procedure TForm1.Button1Click(Sender
var
AComponent : TComponent;
begin
if(Assigned(button3)) then
showmessage(works);
end;
procedure TForm1.Button4Click(Sender
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!
ASKER
^
Bump. I will give extra points to whoever solves this issue.
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('bu tton3')) then
with tbutton.Create(self) do
begin
Parent := self;
visible := true;
name := 'button3';
end;
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
if Assigned(findcomponent('bu tton3')) then
showmessage(works);
end;
procedure TForm1.Button4Click(Sender : TObject);
begin
if Assigned(findcomponent('bu tton3')) then
findcomponent('button3').f ree;
end;
end.
just not tested
meikl ;-)
var
Form1: TForm1;
//button3: tbutton; //not needed
...
procedure TForm1.Button2Click(Sender
begin
if not assigned(findcomponent('bu
with tbutton.Create(self) do
begin
Parent := self;
visible := true;
name := 'button3';
end;
end;
procedure TForm1.Button1Click(Sender
begin
if Assigned(findcomponent('bu
showmessage(works);
end;
procedure TForm1.Button4Click(Sender
begin
if Assigned(findcomponent('bu
findcomponent('button3').f
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
>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.
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.
ASKER
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;
procedure TForm1.Button4Click(Sender
begin
button3.Free;
button3 := nil;
end;
ASKER
Great! Thanks cloadking_device!
Visit this: https://www.experts-exchange.com/questions/20738501/Points-for-cloaking-device.html for your points.
Visit this: https://www.experts-exchange.com/questions/20738501/Points-for-cloaking-device.html for your points.
AComponent : TComponent;
begin
AComponent := FindComponent('AComponentN
if assigned(AComponent) then //it exists
...
meikl ;-)