Solved

accessing multiple components on a form

Posted on 1997-08-25
7
180 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

10 Experts available now in Live!

Get 1:1 Help Now