Home > database >  Disable all objects/components on a panel
Disable all objects/components on a panel

Time:09-27

I have a panel containing labels, buttons, memo's, etc. and I want every component to be disabled on that panel, individually (i.e., Enabled is false).

I want the components to be disabled individually (and not just the panel itself to be disabled) to show the person using my program that the objects are there but cannot be used (they are greyed out because the Enabled property is false).

Is there a quicker way of disabling all of them at once, instead of changing the Enabled property to false for each component?

CodePudding user response:

If you don't want the option to disable the panel, the other option is use ControlsCount and Controls[i] to loop through all the components inside the Panel.

With code like this you can do it:

procedure TForm3.DisableAll(pnl: TPanel);
var
   i:integer;
begin
   for i := 0 to (pnl.ControlCount - 1) do
     SetPropValue(pnl.Controls[i], 'Enabled', False);

There may be components that do not have the Enabled property (such as a TBevel) and in that case it would give an error; To do this you can check if each component you run has it (GetPropInfo):

  for i := 0 to (pnl.ControlCount - 1) do
    if Assigned(GetPropInfo(pnl.Controls[i],  'Enabled')) then
      SetPropValue(pnl.Controls[i],  'Enabled', False);
  

There can also be another TPanel(panel2) inside the original TPanel (panel1) with more components, as in the image. In that case this code would only disable the first ones and Panel2 (not the components inside the panel2). As seen in the image.

enter image description here

If you want it to run recursively, you'll need to run the function recursively. Something like this:

procedure TForm3.DisableAll(pnl: TPanel);
var
  i:integer;
  info:PPropInfo;
begin
  for i := 0 to (pnl.ControlCount - 1) do
    if (pnl.Controls[i] is TPanel) then
      DisableAll(TPanel(pnl.Controls[i]))
    else
      if Assigned(GetPropInfo(pnl.Controls[i], 'Enabled')) then
        SetPropValue(pnl.Controls[i], 'Enabled', False);
end;

And the result will be something like this: enter image description here

CodePudding user response:

Is there a quicker way of disabling all of them at once, instead of changing the Enabled property to false for each component?

The fastest way is to just disable the Panel itself, but you ruled out that possibility. So no, there is no other way. You must loop through the Panel's components and disable them one at a time.

  • Related