0

I have 10 TEdits and 10 images and I tried to use this:

procedure TForm1.Button1Click(Sender: TObject; edit: TEdit);
var i:integer;
begin
for i:= 1 to 10 do
  begin
   if edit[i].text:='1' then picture[i].Show else picture[i].Hide;
  end;
end;

But delphi returns this error: [Error] Unit1.pas(84): Class does not have a default property

I also tried this:

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
for i:= 1 to 10 do
  begin
   if edit[i].text:='1' then picture[i].Show else picture[i].Hide;
  end;
end;

But, again, Delphi returns errors:

[Error] Unit1.pas(84): Undeclared identifier: 'edit'.

Can you help me with this code?

2

2 Answers 2

5

You need to declare and populate your own arrays, and then you can loop through them when needed, eg:

type
  TForm1 = class(TForm)
    Button1: TButton;
    ...
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    // and so on ...
    Image1: TImage;
    Image2: TImage;
    Image3: TImage;
    // and so on ...
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    ...
  private
    edit: array[1..10] of TEdit;
    picture: array[1..10] of TImage;
    ...
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  edit[1] := Edit1;
  edit[2] := Edit2;
  edit[3] := Edit3;
  // and so on...
  picture[1] := Image1;
  picture[2] := Image2;
  picture[3] := Image3;
  // and so on...

  {
  alternatively:

  for I := 1 to 10 do
  begin
    edit[i] := TEdit(FindComponent('Edit' + IntToStr(i)));
    picture[i] := TImage(FindComponent('Image' + IntToStr(i)));
  end;
  }
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I: integer;
begin
  for I := 1 to 10 do
  begin
   if edit[i].Text = '1' then
     picture[i].Show
   else
     picture[i].Hide;
   {
   alternatively:
   picture[i].Visible := (edit[i].Text = '1');
   }
  end;
end;
Sign up to request clarification or add additional context in comments.

Comments

5

You can do one of two things:

  1. Use the form's Components list and FindComponent:

    procedure TForm1.Button1Click(Sender: TObject);
    var 
      i: Integer;
      TempEdit: TEdit;
    begin
      for i:= 1 to 10 do
      begin
        TempEdit := Self.FindComponent('Edit' + IntToStr[i]) as TEdit;
        if Assigned(TempEdit) then
        begin
          if TempEdit.Text = '1' then
            Picture[i].Show
          else
            Picture[i].Hide;
        end;
      end;
    end;
    
  2. Maintain a separate array of your TEdit controls, and reference them directly. When you assign Picture[i], add the matching TEdit to Edits[i], and then

    procedure TForm1.Button1Click(Sender: TObject);
    var 
      i: Integer;
    begin
      for i:= 1 to 10 do
      begin
        if Edits[i].Text = '1' then
            Picture[i].Show
          else
            Picture[i].Hide;
        end;
      end;
    end;
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.