English 中文(简体)
How to use argument in a cast with Delphi
原标题:

How to do this in Delphi:

procedure ToggleVisibility(ControlClass : TControlClass);
var
  i : integer;
begin
  for i := 0 to ComponentCount - 1 do
    if Components[i] is ControlClass then
      ControlClass(Components[i]).Visible := not Control(Components[i]).Visible;
end;

Compiler doesn t allow the cast in this case. Any ideas?

I m using Delphi 2007.

最佳回答
(Components[i] as ComponentClass).Visible
问题回答

Since the component is a TControl or a descendant you have to cast to TControl:

procedure ToggleVisibility(ComponentClass : TControlClass);
var
  i : integer;
begin
  for i := 0 to ComponentCount - 1 do begin
    if Components[i] is ComponentClass then
      TControl(Components[i]).Visible := not TControl(Components[i]).Visible;
  end;
end;

It does not make sense to cast ComponentClass(Components[i]).Visible, because .Visible needs to be of a specific class, in order to be compiled properly. Therefore, you need to specify the exact class that should be cast to. For instance, if TControl has a .Visible property, but a derived class creates a new kind of .Visible property, the compiler would not know, which of these two properties it should compile for.

So the question is, do you want to invert the TControl.Visible, then you should write (Components[i] as TControl).Visible. I guess that s what you want.

If you want to invert the .Visible of any TControl descendent, no matter if it relates to the control being Visible or not, and no matter if it is related to TControl.Visible or not, then you should go for the RTTI solution described elsewhere.

Try this option using the RTTI

Uses
 TypInfo;

procedure TForm1.ToggleVisibility(ComponentClass: TClass);
var
  i       : integer;
  PropInfo: PPropInfo;
  aValue  : Variant;
begin
  for i := 0 to ComponentCount - 1 do
    if Components[i] is ComponentClass then
     begin
      PropInfo := GetPropInfo(Components[i].ClassInfo,  Visible );
      if Assigned(PropInfo) then
      begin
       aValue:=GetPropValue(Components[i],  Visible );
       if PropInfo.PropType^.Kind=tkEnumeration then //All enumerated types. This includes Boolean, ByteBool, WordBool, LongBool and Bool
       SetOrdProp(Components[i], PropInfo, Longint(not Boolean(aValue)));
      end;
     end;
end;

To execute

ToggleVisibility(TEdit);




相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签