Solved

accessing multiple components on a form

Posted on 1997-08-25
7
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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