Recent

Author Topic: [SOLVED] problem with frames in a flowpanel  (Read 413 times)

kirchfritz

  • New Member
  • *
  • Posts: 28
  • WIN10 LAZ 2.0.10 FPC 3.2.0
[SOLVED] problem with frames in a flowpanel
« on: January 21, 2020, 09:33:33 am »
Hi,

I put frame-objects into a tflowpanel.

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.Button1Click(Sender: TObject);
  2. var
  3.   aFrame : TMyFrame;
  4. begin
  5.   LastID := LastID + 1;
  6.   aFrame := TMyFrame.Create(FlowPanel1);
  7.   aFrame.Parent := FlowPanel1;
  8.   aFrame.AutoSize := True;
  9.   aFrame.Align := alNone;
  10.   aFrame.Name  := 'MyFrame'+IntToStr(LastID);
  11. end;  
  12.  

After this I want to call a method for all of these frames of the flowpanel.controllist.
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.Button2Click(Sender: TObject);
  2. var i : integer;
  3.     t : TFlowPanelControl;
  4. begin
  5.   for i := 0 to FlowPanel1.ControlList.Count-1 do
  6.   begin
  7.     t := TFlowPanelControl(FlowPanel1.ControlList.Items[i]);
  8.     TMyFrame(t).SetShapeColor(clYellow); <<<<<<<<<<<<<<<<<<<<<<<<<<< here the program crashes!!!!!!
  9.   end;
  10. end;

MyFrame is defined here:
Code: Pascal  [Select][+][-]
  1. unit uFrame;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Graphics, Controls, BCLabel, BGRAShape,
  9.   DTAnalogClock;
  10.  
  11. type
  12.  
  13.   { TMyFrame }
  14.  
  15.   TMyFrame = class(TFrame)
  16.     BCLabel1: TBCLabel;
  17.     BCLabel2: TBCLabel;
  18.     BGRAShape1: TBGRAShape;
  19.     DTAnalogClock1: TDTAnalogClock;
  20.     procedure BGRAShape1Click(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.     procedure SetShapeColor(aColor : TColor);
  25.   end;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TMyFrame }
  32.  
  33. procedure TMyFrame.BGRAShape1Click(Sender: TObject);
  34. begin
  35.   SetShapeColor(clBlue);
  36. end;
  37.  
  38. procedure TMyFrame.SetShapeColor(aColor: TColor);
  39. begin
  40.   Self.BGRAShape1.FillColor := aColor;
  41. end;
  42.  
  43. end.
  44.  
  45.  

Unfortunately the program crashes, because the typecast from TFlowPanelControl to TMyFrame doesnt work.

Can anybody help me?
You will find my sourcode in appendix

Kind regards
Fritz
« Last Edit: January 21, 2020, 10:26:13 am by kirchfritz »

wp

  • Hero Member
  • *****
  • Posts: 7620
Re: problem with frames in a flowpanel
« Reply #1 on: January 21, 2020, 10:03:27 am »
SHIFT-click on TFlowPanel in your code and the IDE will open the unit in which TFlowPanel is implemented. There you'll see:
  • FlowPanel.ControlList is a TFlowPanelControlList, i.e. a list of TFlowPanelControl items.
  • TFlowPanelControl is a TCollectionItem, i.e. it never can be cast to a TControl. However, there is a Control property. Therefore, this modified code should work (not tested):
Code: Pascal  [Select][+][-]
  1.     procedure TfrmMain.Button2Click(Sender: TObject);
  2.     var i : integer;
  3.         t : TFlowPanelControl;
  4.     begin
  5.       for i := 0 to FlowPanel1.ControlList.Count-1 do
  6.       begin
  7.         t := TFlowPanelControl(FlowPanel1.ControlList.Items[i]);  // BTW: Type-cast not needed here
  8.         TMyFrame(t.Control).SetShapeColor(clYellow);   // <--- ".Control" added
  9.       end;
  10.     end;
« Last Edit: January 21, 2020, 10:19:27 am by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

luca

  • Jr. Member
  • **
  • Posts: 57
Re: problem with frames in a flowpanel
« Reply #2 on: January 21, 2020, 10:12:31 am »
As alternative you can loop through all the controls in the panel:
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.Button2Click(Sender: TObject);
  2. var i : integer;
  3.     t : TControl;
  4. begin
  5.   for i := 0 to FlowPanel1.ControlCount-1 do
  6.   begin
  7.     t := FlowPanel1.Controls[i];
  8.     if t is TMyFrame then
  9.      TMyFrame(t).SetShapeColor(clYellow);
  10.   end;
  11. end;
  12.  

in this case you've to check if the control is really a TMyFrame.

Regards
Luca

kirchfritz

  • New Member
  • *
  • Posts: 28
  • WIN10 LAZ 2.0.10 FPC 3.2.0
[SOLVED] problem with frames in a flowpanel
« Reply #3 on: January 21, 2020, 10:25:30 am »
thanks luca, thanks wp!
Both answers solved my problem!

 

TinyPortal © 2005-2018