All controls that can be parents for other controls have as common ancestor the TWinVontrol.
this makes things easier to handle for example
function EnumControls(const aControl:TWinControl; const StartSpace :string=''):String;
var
Res : string;
begin
for Cnt := 0 to aControl.ControlCount -1 do
begin
WriteStr(Res,StartSpace + aControl.Controls[Cnt].Name);
Result:= Result + Res + LineEnding;
if aControl.Controls[Cnt] is TWinControl then
EnumControls(TwinControl(aControl.Controls[Cnt]), StartSpace+' ')
end;
end;
as you can see I use the is keyword that tests if a variable supports the type I want and returns true or false. It is far better than the as keyword which equivalent to TWonControl(MyVariable) hard cast.
Your two problem are that
1) try..finally is not designed to stop exception just to give you the chance to clean up if anything goes wrong. to stop an exception you need to use try..except blocks.
2) the as keyword is deisgned to raise an exception if a control does not support the cast you are trying to support.
regards
jo