AlphaMc111
AlphaMc111

Reputation: 47

How check all TImage in Form

What I would like to do is run a if statement that checks if any TImage object in the form has a .top property equal to Tedit1.text. Then I want to change a the .image property of the TImage that matched the variable to /Images/ButtonStand/jpeg

Upvotes: 0

Views: 377

Answers (1)

Ken White
Ken White

Reputation: 125748

You can loop through the Form.Components to locate it. (You didn't specify where you'd get the variable for .Top, so I've just retrieved it from a TEdit. You also didn't specify where your new image would come from, so I've presumed it's already loaded into a TBitmap somewhere.)

procedure TForm1.ChangeImageButtonClick(Sender: TObject);
var
  i: Integer;
  TopValue: Integer;
  NewBmp: TBitmap;
begin
  // Get top value to locate from Edit1
  TopValue := StrToIntDef(Edit1.Text, -1);  
  if TopValue = -1 then  // User entered invalid value. Nothing to do; bail out.
    Exit;

  for i := 0 to ComponentCount - 1 do
  begin
    if Components[i] is TImage then
    begin
      if TImage(Components[i]).Top = TopValue then
      begin
        NewBmp := TBitmap.Create;
        NewBmp.LoadFromFile('Images' + IntToStr(i) + '.bmp');
        try
          TImage(Components[i]).Picture.Graphic.Assign(NewBmp);
        finally
          NewBmp.Free;
        end;
      end;
    end;
  end;
end;

Upvotes: 3

Related Questions