nyt
asked on
Simple question
Hi there,
How can I create objects (say buttons) at runtime? Normally, we create buttons by this syntax:
NEWBTN:=TButton.Create(Sel f);
But now I want the buttons to be created only when the user requests, and the names of the buttons are specified by the user, say through Edit1.Text
Two problems:
1. How can I create a button with the name given by the user?
2. Later, the user will give another name in Edit2.Text, how can I search whether there is a button with the name matched with Edit2.Text? If that button exists, how can I delete it?
How can I create objects (say buttons) at runtime? Normally, we create buttons by this syntax:
NEWBTN:=TButton.Create(Sel
But now I want the buttons to be created only when the user requests, and the names of the buttons are specified by the user, say through Edit1.Text
Two problems:
1. How can I create a button with the name given by the user?
2. Later, the user will give another name in Edit2.Text, how can I search whether there is a button with the name matched with Edit2.Text? If that button exists, how can I delete it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
about the second problem:
...
var
I: Integer;
Temp: TComponent;
begin
for I := Form1.ComponentCount - 1 downto 0 do
begin
Temp := Form1.Components[I];
if Temp is TButton then
if Temp.Name = Edit2.Text then
begin
TButton(temp).Free;
end;
end;
end;
...
...
var
I: Integer;
Temp: TComponent;
begin
for I := Form1.ComponentCount - 1 downto 0 do
begin
Temp := Form1.Components[I];
if Temp is TButton then
if Temp.Name = Edit2.Text then
begin
TButton(temp).Free;
end;
end;
end;
...
I addressed both the parent issue and the freeing of the button in my code. Plerase try it out and see if it is not what you're looking to do.
var UserChoice:string;
......
NEWBTN:=TButton.Create(Sel f);
NEWBTN.Caption:=UserChoice ;
.....
procedure TForm1.Button1Click(Sender : TObject);
var
i:integer;
Component : TComponent;
z:string;
begin
i:=0;
while i<ComponentCount do
begin
Component := Components[i];
if (Component is TButton) then begin
if ((Component as TButton).Caption='Button2' ) then
begin
(Component as TButton).Free;
i:=ComponentCount;
end
else
inc(i);
end
else
inc(i);
end;
end;
......
NEWBTN:=TButton.Create(Sel
NEWBTN.Caption:=UserChoice
.....
procedure TForm1.Button1Click(Sender
var
i:integer;
Component : TComponent;
z:string;
begin
i:=0;
while i<ComponentCount do
begin
Component := Components[i];
if (Component is TButton) then begin
if ((Component as TButton).Caption='Button2'
begin
(Component as TButton).Free;
i:=ComponentCount;
end
else
inc(i);
end
else
inc(i);
end;
end;
ASKER
DrDelphi,
Do you write your code in D5 or D6? My D4 can't compile them.
Undeclared identifier: 'ClickHandler'
Undeclared identifier: 'FreeandNil'
Do you write your code in D5 or D6? My D4 can't compile them.
Undeclared identifier: 'ClickHandler'
Undeclared identifier: 'FreeandNil'
ASKER
DrDelphi,
Do you write your code in D5 or D6? My D4 can't compile them.
Undeclared identifier: 'ClickHandler'
Undeclared identifier: 'FreeandNil'
Do you write your code in D5 or D6? My D4 can't compile them.
Undeclared identifier: 'ClickHandler'
Undeclared identifier: 'FreeandNil'
ASKER
Well...after seeing all the comments, I finally solved the problem.
Here is a new problem: how can I add something to the onClick event (and other events) of those newly created buttons? Say, I want ShowMessage('testing') when the buttons are clicked.
More points will be added according to the complexity of the answers.
Here is a new problem: how can I add something to the onClick event (and other events) of those newly created buttons? Say, I want ShowMessage('testing') when the buttons are clicked.
More points will be added according to the complexity of the answers.
That's what DrDelphi wrote:
with btnarray[length(btnarray)- 1]do
begin
Top:=10;
Caption:='DrDelphi';
OnClick:=ClickHandler; //event handler
Parent:=self;///very important to see the button!!
name:='Button3';
end;
You should write this code:
with btnarray[length(btnarray)- 1]do
begin
Top:=10;
Caption:='DrDelphi';
OnClick:=ClickHandler; //event handler
Parent:=self;///very important to see the button!!
name:='Button3';
OnClick:=MyBtnClick; { !!!!!! }
end;
procedure MyBtnClick(Sender As TButton);
begin
showmessage('this is a test');
end;
with btnarray[length(btnarray)-
begin
Top:=10;
Caption:='DrDelphi';
OnClick:=ClickHandler; //event handler
Parent:=self;///very important to see the button!!
name:='Button3';
end;
You should write this code:
with btnarray[length(btnarray)-
begin
Top:=10;
Caption:='DrDelphi';
OnClick:=ClickHandler; //event handler
Parent:=self;///very important to see the button!!
name:='Button3';
OnClick:=MyBtnClick; { !!!!!! }
end;
procedure MyBtnClick(Sender As TButton);
begin
showmessage('this is a test');
end;
Sorry,
procedure MyBtnClick(Sender As TButton);
should be
procedure MyBtnClick(Sender: TButton);
procedure MyBtnClick(Sender As TButton);
should be
procedure MyBtnClick(Sender: TButton);
I mean that
OnClick:=MyBtnClick;
comes instead of
OnClick:=ClickHandler;
So the final answer is:
with btnarray[length(btnarray)- 1]do
begin
Top:=10;
Caption:='DrDelphi';
Parent:=self;
name:='Button3';
OnClick:=MyBtnClick;
end;
procedure MyBtnClick(Sender As TButton);
begin
showmessage('this is a test');
end;
OnClick:=MyBtnClick;
comes instead of
OnClick:=ClickHandler;
So the final answer is:
with btnarray[length(btnarray)-
begin
Top:=10;
Caption:='DrDelphi';
Parent:=self;
name:='Button3';
OnClick:=MyBtnClick;
end;
procedure MyBtnClick(Sender As TButton);
begin
showmessage('this is a test');
end;
The assumption was that ClickHandler is a procedure to handle the OnClick event of the button. As for FreeAndNil, that came into being in Delphi 5... for D4, use Free and then Obj:=nil (whre obj is a pointer to the button).
Good luck!!
Good luck!!
You can simply assign the Name property of the button like any other property.
Form.Components and Form.ComponentCount allows to iterate all components on the Form.