Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

accessing multiple components on a form

Posted on 1997-08-25
7
Medium Priority
?
191 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 100 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

916 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