Solved

Urgent: Retrieve a control in runtime application

Posted on 2002-05-15
5
185 Views
Last Modified: 2010-04-04
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].ClassName  ;
      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
0
Comment
Question by:cekcool
5 Comments
 
LVL 6

Accepted Solution

by:
swift99 earned 50 total points
ID: 7012941
Just a couple of subtle things to keep in mind - VB controls are all ActiveX.  Delphi controls are not necessarily (very rarely) ActiveX, so you may have to explicitly cast them

Note that I am using the "is" operator instead of using the control ClassName property.  Apart from performance issues (quick pointer comparisions versus string operations), this ensures that you can use any descendents of the TCheckbox with full transparency.

var
  aCtrl: TObject;

// To set all checkboxes Checked = true

for ctrls := 0 to Form1.ControlCount-1 do
begin
  aCtrl := Form1.Controls[ctrls];
  if aCtrl is TCheckBox then begin
    (aCtrl as TCheckbox).Checked := true;
  end;
end;

// To review the checkboxes
for ctrls := 0 to Form1.ControlCount-1 do
begin
  aCtrl := Form1.Controls[ctrls];
  if aCtrl is TCheckBox then begin
    case (aCtrl as TCheckbox).Checked of
    true: ; // do something
    false:; // do something else
    end;
  end;
end;

0
 
LVL 4

Expert Comment

by:nestorua
ID: 7014650
HI, cekcool,
procedure ShowAllCheckedCheckBoxesOnTheForm;
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.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7015917
another way

for i := 0 to componentcount - 1 do
  if components[i] is tcheckbox then
    if tcheckbox(components[i]).name = 'chk_1' then
      tcheckbox(components[i]).checked := 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 ;-)

0
 
LVL 34

Expert Comment

by:Slick812
ID: 7019253
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[CBNum-1].Height+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
0
 

Author Comment

by:cekcool
ID: 7058021
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, MySQL Query 4 164
Delphi selector screen 2 84
Print Graphic and Text to Epson TM-T88v 12 373
How to build JSON File in Delphi 6 3 45
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question