문제

나는 그것에 많은 컨트롤이있는 양식을 가지고 있으며, 특정 패널의 모든 컨트롤을 반복하여 활성화/비활성화하고 싶었습니다.

나는 이것을 시도했다 :

var component: TComponent;
begin
  for component in myPanel do
    (component as TControl).Enabled := Value;
end;

그러나 그것은 아무것도하지 않았다. 모든 구성 요소는 부모 객체가 아닌 양식의 구성 요소 모음에 있습니다. 그렇다면 모든 컨트롤을 컨트롤 내부에 넣을 방법이 있는지 아는 사람이 있습니까? (이와 같은 못생긴 해결 방법 외에, 결국 내가해야 할 일입니다) :

var component: TComponent;
begin
  for component in myPanel do
    if (component is TControl) and (TControl(component).parent = myPanel) then
      TControl(component).Enabled := Value;
end;

누군가 더 나은 방법이 있다고 말 해주세요 ...

도움이 되었습니까?

해결책

당신은 찾고 있습니다 TWinControl.Controls 배열과 동반 ControlCount 재산. 그것들은 통제의 직계 아이들을위한 것입니다. 손자 등을 얻으려면 표준 재귀 기술을 사용하십시오.

당신은 정말로 원하지 않습니다 Components 배열 (이것은 무엇입니다 for-in 루프는 일반적으로 부모-자식 관계와 할 일이 없기 때문에 반복됩니다. 구성 요소는 아동 관계가없는 물건을 소유 할 수 있으며 통제는 소유하지 않은 어린이를 가질 수 있습니다.

또한 제어를 암시 적으로 비활성화하면 모든 어린이도 비활성화됩니다. 장애인 통제의 어린이와 상호 작용할 수 없습니다. OS는 입력 메시지를 보내지 않습니다. 그들을 만들기 위해 바라보다 그러나 비활성화 된 경우 별도로 비활성화해야합니다. 즉, 버튼에 회색 텍스트를 만들려면 버튼이 마우스 클릭에 응답하지 않더라도 부모를 비활성화하는 것만으로는 충분하지 않습니다. 버튼 자체를 "비활성화"로 페인트로 만들려면 버튼 자체를 비활성화해야합니다.

다른 팁

패널을 비활성화하면 AL 컨트롤도 비활성화됩니다.

익명 방법을 갖춘 재귀 솔루션 :

type
  TControlProc = reference to procedure (const AControl: TControl);

procedure TForm6.ModifyControl(const AControl: TControl; 
  const ARef: TControlProc);
var
  i : Integer;
begin
  if AControl=nil then
    Exit;
  if AControl is TWinControl then begin
    for i := 0 to TWinControl(AControl).ControlCount-1 do
      ModifyControl(TWinControl(AControl).Controls[i], ARef);
  end;
   ARef(AControl);
end;

procedure TForm6.Button1Click(Sender: TObject);
begin
  ModifyControl(Panel1,
    procedure (const AControl: TControl)
    begin
      AControl.Enabled := not Panel1.Enabled;
    end
  );
end;

다음은 Delphi 2007 방식입니다.

procedure TForm6.ModifyControl(const AControl: TControl; const value: Boolean);
var
  i: Integer;
begin
  if AControl=nil then Exit;
  if AControl is TWinControl then begin
    for i := 0 to TWinControl(AControl).ControlCount-1 do
      ModifyControl(TWinControl(AControl).Controls[i], value);
  end;
  Acontrol.Enabled := value;
end;

procedure TForm6.Button1Click(Sender: TObject); 
begin 
  ModifyControl(Panel1, true);  // true or false
end;

간단히

Panel.Enabled := Value;

나는이 게시물이 조금 오래되었다는 것을 알고 있지만 동일한 정보를 검색하여 여기에 왔습니다. 다음은 관심있는 사람을 위해 운동 한 C ++ 코드입니다.

// DEV-NOTE:  GUIForm flattens the VCL controls
// VCL controls are nested.  I.E. Controls on a
// Panel would have the Panel as a parent and if
// that Panel is on a TForm, TForm's control count
// does not account for the nested controls on the
// Panel.
//
// GUIControl is passed a Form pointer and an index
// value, the index value will walk the controls on the
// form and any child controls counting up to the idx
// value passed in.  In this way, every control has a
// unique index value
//
// You can use this to iterate over every single control
// on a form.  Here is example code:
//
// int count = 0;
// TForm *pTForm = some_form
// TControl *pCtrl = 0;
// do
// {
//      pCtrl = GUIControl(pTForm, count++);
//
// }while(pCtrl);

TControl *GUIControl(TForm *F, int idx)
{
    TControl *rval = 0;
    int RunCount = 0;

    for(int i=0; i<F->ControlCount && !rval; i++)
    {
        TControl *pCtl = F->Controls[i];

        if(RunCount == idx )
            rval = pCtl;
        else
            rval = GUIChildControl( pCtl, RunCount, idx);

        RunCount++;
    }

    return(rval);
}

TControl *GUIChildControl(TControl *C, int &runcount, int idx)
{
    TControl *rval = 0;
    TWinControl *pC = dynamic_cast<TWinControl *>(C);
    if(pC)
    {
        for(int i=0; i<pC->ControlCount && !rval; i++)
        {
            TControl *pCtrl = pC->Controls[i];
            runcount++;

            if( runcount == idx)
                rval = pCtrl;
            else
            {
                TWinControl *pCC = dynamic_cast<TWinControl *>(pCtrl);

                if(pCC)
                {
                    if( pCC->ControlCount )
                        rval = GUIChildControl(pCtrl, runcount, idx);
                }
            }
        }
    }

    return(rval);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top