본문 바로가기
Delphi Tip/컴포넌트

FindComponent를 활용한 동적 컴포넌트 접근

by MonoSoft 2025. 4. 26.
728x90
반응형

FindComponent를 활용한 동적 컴포넌트 접근

 

 

델파이에서는 FindComponent를 이용해 
런타임 중 컴포넌트 이름을 문자열로 찾아 접근할 수 있습니다. 
폼에 많은 컴포넌트가 있을 때 동적으로 제어하거나, 
이름 규칙에 따라 그룹 작업을 할 때 유용합니다.

procedure SetLabelCaptions;
var
  I: Integer;
  Lbl: TLabel;
begin
  for I := 1 to 5 do
  begin
    Lbl := TLabel(FindComponent('Label' + IntToStr(I)));
    if Assigned(Lbl) then
      Lbl.Caption := '항목 ' + IntToStr(I);
  end;
end;

FindComponent는 폼에 존재하는 컴포넌트를 이름으로 찾는다.
반환형이 TComponent이므로, 타입 변환(casting)이 필요하다.
존재 여부를 반드시 Assigned로 확인해야 안전한 코드가 된다.

 


#델파이
#Delphi
#FindComponent
#동적컴포넌트
#런타임제어
#TComponent
#VCL개발
#컴포넌트접근
#폼컨트롤
#UI자동화

728x90
반응형

댓글