PDA

View Full Version : Lazarus TWinControl TImage



De-Panther
03-06-2008, 09:45 AM
Compiler/IDE: Lazarus
Libraries/API: LCL

To change the caption of labels i'm using:

for i:=1 to 10 do
formname.ControlByName('tlabelname'+inttostr(i)).c aption:='';


I tryed to use the same way to load image

for i:=1 to 10 do
formname.ControlByName('timagename'+inttostr(i)).p icture.LoadFromFile('file.bmp');


I looked in the LCL reference and saw that thers no method like this by useing ControlByName.

Does someone has any idea how to load image from file to list of Timages by their names?

JSoftware
03-06-2008, 03:54 PM
FindComponent I think it's called

De-Panther
05-06-2008, 06:01 PM
thanks

It's bringing back TComponent - wich means the TImage class.
But still... I can't use it. I think findcomponent it's for the management of the components hirarchy. not for editing them.


meanwhile I used case of. but it's just doing the code ugly(i'll need to do lots of changeing if i'll want to add more TImages)

Senap
05-06-2008, 11:08 PM
I haven't tried Lazarus but shouldn't something like this work?


var Component: TComponent;
begin
Component := FindComponent('Image1');

if Assigned(Component) then
TImage(Component).Canvas.FillRect(TImage(Component ).ClientRect);

HopeDagger
06-06-2008, 02:25 AM
You managed to change the captions because all controls have this property. Not all controls have a 'Picture' member, so you need to type-cast the control to a TImage, first. Even though you already know that it's a TImage, the compiler can only assume (unless you tell it otherwise) that it's just a regular control object.


for i:=1 to 10 do
TImage(formname.ControlByName('timagename'+inttost r(i))).Picture.LoadFromFile('file.bmp');

De-Panther
06-06-2008, 10:30 AM
You managed to change the captions because all controls have this property. Not all controls have a 'Picture' member, so you need to type-cast the control to a TImage, first. Even though you already know that it's a TImage, the compiler can only assume (unless you tell it otherwise) that it's just a regular control object.


for i:=1 to 10 do
TImage(formname.ControlByName('timagename'+inttost r(i))).Picture.LoadFromFile('file.bmp');

Thanks
It works^_^