Solved

accessing multiple components on a form

Posted on 1997-08-25
7
189 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
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!

 

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

623 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