TControl as TPicture?
I am walking a form's control list and when I find a TPicture I want to change a property (image, but anything will do for an example).
How do I开发者_高级运维 code that? It seems like TPciture and TControl are not compatible. Can I cast somehow?
Update: when I add this code
for i:= 0 to Pred(designerForm.ControlCount) do
if designerForm.Controls[i] is TPicture then
MessageDlg('Picture : ' + designerForm.Controls[i].name,
mtInformation, [mbOK], 0);
where designerForm is TForm, I go from zero to two errors in D7
[Error] PictureInspEditor.pas(121): Incompatible types: 'TPicture' and 'TControl'
[Fatal Error] E_logger.dpr(22): Could not compile used unit 'PictureInspEditor.pas'
Are you sure when you say TPicture
maybe do you really want to say TImage
?, anyway in any case first you must check if the current control is of the class which you want evaluate TImage
(in this case) then you can cast in this way TImage(Controls[i])
check this sample for a TImage
var
i : Integer;
begin
for i := 0 to ControlCount-1 do
if Controls[i] is TImage then
begin
// do your stuff here
TImage(Controls[i]).Picture:=aValue;
end;
end;
UPDATE
you can't compare the TPicture
class against the TControl
using the is operator
because the type of the TPicture
is unrelated to the TControl
.
from the Embarcadero documentation
The is Operator .....If the declared type of object is unrelated to class - that is, if the types are distinct and one is not an ancestor of the other a compilation error results
As TPicture doesn't descend from TControl, Controls[i] is TPicture
is invalid.
In D2007 TPicture descends from TInterfacedPersistent->TPersistent->TObject
TPicture = class(TInterfacedPersistent, IStreamPersist)...
As it is not a TControl, casting to a TControl would cause very bad things (probably AV's)
As RRUZ says, you probably want TImage instead, which has a TPicture as its Picture property.
It's been awhile on this, but it popped up in a related search so I'll take a stab at it... Ymmv :-).
The correct comparison is, I believe, in:
for i:= 0 to Pred(designerForm.ControlCount) do
if designerForm.Controls[i] is TPicture then
MessageDlg('Picture : ' + designerForm.Controls[i].name,
mtInformation, [mbOK], 0);
if TObject(designerForm.Controls[i]) is TPicture then
so the two objects have shared ancestor from the compiler's perspective. While TPersistent could also be used as the cast, the VCL implementation exposes methods that allow any object to be in the Tlist the Controls property uses, so TObject is safer.
That this can happen is nominally a bug starting in Delphi 1, iirc, that qualifies as feature now since the VCL controls dropped from the IDEs palette usually load and save as expected. Making reliable use of it as a feature requires careful programming that isn't described, however, nor will I attempt to do so here. The manuals only warn, to paraphrase, "!Don't! modify the list, treat it as read-only; let the IDE manage it via property editors", so TPersistent and descendants the IDE uses that aren't in the VCL can skip a lot of sanity checks.
精彩评论