cekcool
asked on
Urgent: Retrieve a control in runtime application
Can some one help me to do this:
I had dinamically created several component (CheckBox) during the runtime. So, i need to check the "Checked" property of the checkboxes after a buttonclick, but i can't get the component by code.
My Code:
//Form1
var
i: integer;
Begin
i =0;
//this code repeat several times
With TCheckBox.Create(Self) do
begin
Name := 'chk_' + IntToStr(i+1);
...
end;
// I try to do it this way...
procedure TForm1.btnClick(Sender: TObject);
var
ctrls, i : Integer;
CtrlName : string;
begin
ctrls_No := 1;
for ctrls := 0 to Form1.ControlCount-1 do
begin
CtrlName := Form1.Controls[ctrls].Clas sName ;
if CtrlName = 'TCheckBox' then begin
{
How can I set the "ctrls".Checked = True to this
control?
normal:if chk_1.Checked then begin ..do something
Current situation: How??
}
end;
end;
...
I plan to use some codes like in the VB:
foreach ctrl in ctrls
if ctrl.checked = true then
//...do someting
end if
end if
I had dinamically created several component (CheckBox) during the runtime. So, i need to check the "Checked" property of the checkboxes after a buttonclick, but i can't get the component by code.
My Code:
//Form1
var
i: integer;
Begin
i =0;
//this code repeat several times
With TCheckBox.Create(Self) do
begin
Name := 'chk_' + IntToStr(i+1);
...
end;
// I try to do it this way...
procedure TForm1.btnClick(Sender: TObject);
var
ctrls, i : Integer;
CtrlName : string;
begin
ctrls_No := 1;
for ctrls := 0 to Form1.ControlCount-1 do
begin
CtrlName := Form1.Controls[ctrls].Clas
if CtrlName = 'TCheckBox' then begin
{
How can I set the "ctrls".Checked = True to this
control?
normal:if chk_1.Checked then begin ..do something
Current situation: How??
}
end;
end;
...
I plan to use some codes like in the VB:
foreach ctrl in ctrls
if ctrl.checked = true then
//...do someting
end if
end if
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
another way
for i := 0 to componentcount - 1 do
if components[i] is tcheckbox then
if tcheckbox(components[i]).n ame = 'chk_1' then
tcheckbox(components[i]).c hecked := true;
another way
var comp : TComponent; //or tobject
begin
comp := findcomponent('chk_1');
if comp <> nil then
if (comp is tcheckbox) then
tcheckbox(comp).checked := true;
end;
meikl ;-)
for i := 0 to componentcount - 1 do
if components[i] is tcheckbox then
if tcheckbox(components[i]).n
tcheckbox(components[i]).c
another way
var comp : TComponent; //or tobject
begin
comp := findcomponent('chk_1');
if comp <> nil then
if (comp is tcheckbox) then
tcheckbox(comp).checked := true;
end;
meikl ;-)
hello cekcool, if I create a number of VCL components at run time, I usually use an Array of that component, TBitmap, TCheckBox, TButton, ect. That way I can make as many as needed AND not have to name them to keep track of them.
private
{ Private declarations }
CBArray: Array of TCheckBox;
CBNum: Cardinal;
procedure TForm1.FormCreate(Sender: TObject);
begin
CBNum := 0;
end;
procedure TForm1.sbut_AddCheckBClick (Sender: TObject);
begin
Inc(CBNum);
SetLength(CBArray,CBNum);
CBArray[CBNum-1] := TCheckBox.Create(Self);
CBArray[CBNum-1].Parent := Self;
CBArray[CBNum-1].Caption := 'Check Box'+IntToStr(High(CBArray ));
CBArray[CBNum-1].Left := 144;
CBArray[CBNum-1].Top := 6+ (High(CBArray)*(CBArray[CB Num-1].Hei ght+3));
end;
procedure TForm1.sbut_IsCheckedClick (Sender: TObject);
var
i:Integer;
begin
if CBNum > 0 then
begin
if CBArray[0].Checked then
ShowMessage('CheckBox0 is checked');
for i := 0 to High(CBArray) do
CBArray[i].Checked := not CBArray[i].Checked
end;
end;
Hope this Helps, let me know
private
{ Private declarations }
CBArray: Array of TCheckBox;
CBNum: Cardinal;
procedure TForm1.FormCreate(Sender: TObject);
begin
CBNum := 0;
end;
procedure TForm1.sbut_AddCheckBClick
begin
Inc(CBNum);
SetLength(CBArray,CBNum);
CBArray[CBNum-1] := TCheckBox.Create(Self);
CBArray[CBNum-1].Parent := Self;
CBArray[CBNum-1].Caption := 'Check Box'+IntToStr(High(CBArray
CBArray[CBNum-1].Left := 144;
CBArray[CBNum-1].Top := 6+ (High(CBArray)*(CBArray[CB
end;
procedure TForm1.sbut_IsCheckedClick
var
i:Integer;
begin
if CBNum > 0 then
begin
if CBArray[0].Checked then
ShowMessage('CheckBox0 is checked');
for i := 0 to High(CBArray) do
CBArray[i].Checked := not CBArray[i].Checked
end;
end;
Hope this Helps, let me know
ASKER
It does what i want. Thanks. I think may be other answer are also acceptable, but this is the best way to do it right.
procedure ShowAllCheckedCheckBoxesOn
var SL: TStringList;
i: integer;
begin
SL:=TStringList.Create;
try
for i:=0 to ComponentCount-1 do
if Components[i] is TCheckBox
then
with (Components[i] as TCheckBox) do
if Checked
then SL.Add(Name);
//
ShowMessage('Checked are: '+#13+SL.Text);
finally
SL.Free;
end;
end;
It is almost the same as proposed Swift99 but if you have one of your CheckBoxes on the Panel lying on the Form
it will not be caught by the Controls.
Sincerely,
Nestorua.