Solved

accessing multiple components on a form

Posted on 1997-08-25
7
182 Views
Last Modified: 2011-08-18
I am trying to create a simple stationary database for use in my job.I have created a form displaying labels 26 Districts, together with the edit boxes to capture the input. This will then be fed into an array.

Is it possible to cycle through the contents of each
editbox and place them into an array ?

I would be grateful for any help or any pointers.

Thanks

Darren Millin  
0
Comment
Question by:DMillin
7 Comments
 
LVL 1

Accepted Solution

by:
sxh earned 50 total points
ID: 1342495
hi DMillin

If I am correct, you want to cycle through each edit box and place each textual content into an array.


var
  myarray : array[0..25] of string;
  i       : integer;

begin
  for i := 1 to 26 do
    begin
      myarray[i-1] := editbox+i.Text;
    end;
end.

hope this helps

regards

sxh

0
 

Expert Comment

by:krisz
ID: 1342496
The only way to access controls by index is using the form's
Controls and ControlCount property or the similar Components and
ComponentCount if the Form is not the Parent of the controls
(ie. they are on a panel).

Tip: you can use the controls' Tag property to group them.
You can set Tag in desing time.

example:

var
  Idx, i: integer;
  C: TObject;

Idx:= 0;
for i:=0 to MyForm.ComponentCount do begin
  C:= MyForm.Components[i];
  if C is TEdit then begin
    if TEdit(C).Tag= 2 then begin
      MyArray[Idx]:= TEdit(C).Text;
      inc(Idx);
    end;
  end;
end;
0
 

Expert Comment

by:rammstein
ID: 1342497
Another way :))

This routine creates Edit-fields at runtime. You can access them via edit[i].caption.
Yours, rammstein.

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
Edit: array[1..10] of TEdit;
begin
 for i:=1 to 10 do
 begin
  edit[i]:=TEdit.Create(Application);
  with edit[i] do
   begin
   Parent:=Form1;
   Left:=10;
   Top:=i*20;
   Enabled:=TRUE;
   Visible:=TRUE;
   Width:=35;
   Height:=15;
   end;

 end;
end;

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:rammstein
ID: 1342498
Another way :))

This routine creates Edit-fields at runtime. You can access them via edit[i].caption.
Yours, rammstein.

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
Edit: array[1..10] of TEdit;
begin
 for i:=1 to 10 do
 begin
  edit[i]:=TEdit.Create(Application);
  with edit[i] do
   begin
   Parent:=Form1;
   Left:=10;
   Top:=i*20;
   Enabled:=TRUE;
   Visible:=TRUE;
   Width:=35;
   Height:=15;
   end;

 end;
end;

0
 

Expert Comment

by:rammstein
ID: 1342499
OOOOOps.. actually.. you can access these edit-boxes created at runtime via edit[i].text

Yours, rammstein
0
 
LVL 1

Expert Comment

by:sxh
ID: 1342500
here is a modified example that does away with the creation of the TObject variable. It also puts a minus 1 after the componentcount function, this is necessary as the component index is always 1 higher than the total number of components.

hope you don't mind the changes, you are quite right about the component access krisz and deserve the points.

var
  i : integer;
  MyArray : Array[0..?] of string;

begin

  for i := 0 to ComponentCount - 1 do begin
    if Components[i] is TEdit then
      begin
        if TEdit(Components[i]).Tag= i then
          begin
            MyArray[i] := TEdit(Components[i]).Text;
          end;
      end;
end;

regards

sxh
0
 

Expert Comment

by:joseramos
ID: 1342501
Yet another example ...

procedure TForm1.SaveEditText;
var
    I: Integer;
    J: Integer;
    MyArr: Array[0..26] of string;
begin
    J := 0;
   
    with (Owner as TForm) do
    begin
        for I := 0 to ComponentCount - 1 do
        begin
            if Components[I] is TEdit then
            begin
                MyArr[J] := TEdit(Components[I]).Text;
                J := J + 1;                
            end;
       end;
    end;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IExtractImage Delphi 14 207
how do i create updater to My Activex application? 3 79
Magic Software info 18 125
SUM 2 INTEGER ARRAYS INTO 1 10 93
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now