Recent

Author Topic: TPanel at runtime  (Read 2442 times)

seghele0

  • Full Member
  • ***
  • Posts: 173
TPanel at runtime
« on: October 02, 2021, 11:42:23 am »
At runtime !
Try to get a "Label" on a TPanel.
The code appears to be correct but the "Label" isn't displayed.
Please help.
 :-[
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls,
  9.   Graphics, Dialogs, StdCtrls, ExtCtrls, Buttons;
  10.  
  11. type
  12.  
  13.   { TFmain }
  14.  
  15.   TFmain = class(TForm)
  16.     btnSHOWpanel: TButton;
  17.     brnEXIT: TSpeedButton;
  18.     procedure brnEXITClick(Sender: TObject);
  19.     procedure btnSHOWpanelClick(Sender: TObject);
  20.     procedure AnotherButtonClick(Sender: TObject); // eigen proc.
  21.     procedure btnTESTClick(Sender: TObject);    // eigen proc.
  22.  
  23.   private
  24.  
  25.   public
  26.     Pan1: TPanel;
  27.     lblTEXT: TLabel;
  28.     btnJAPRINT: TSpeedButton;
  29.     btnNEENPRINT: TSpeedButton;
  30.   end;
  31.  
  32. var
  33.   Fmain: TFmain;
  34.  
  35. implementation
  36.  
  37. {$R *.frm}
  38.  
  39. { TFmain }
  40.  
  41. // Eigen proc. om het Panel te verwijderen.
  42. // De naam v.d. proc. moet een extra nieuwe naam hebben.
  43. procedure TFmain.AnotherButtonClick(Sender: TObject);
  44. begin
  45.   FreeAndNil(Pan1);
  46. end;
  47.  
  48. // Deze proc. is enkel nodig om een "Showmessage" te tonen, verder niets!
  49. procedure TFmain.btnTESTClick(Sender: TObject);
  50. begin
  51.   Showmessage(' YES button. ');
  52. end;
  53.  
  54. procedure TFmain.btnSHOWpanelClick(Sender: TObject);
  55. begin
  56.     // = Panel op FMain zetten
  57.     Pan1 := TPanel.Create(Self);
  58.     with Pan1 do
  59.     begin
  60.       Parent := Fmain; // Moet de naam v.d. Form.
  61.       Left:= 30;
  62.       Top := 50;
  63.       Width := 500;
  64.       Height := 250;
  65.       Color:= clYellow;
  66.       Caption:= ' Make your choice ';
  67.       Show;
  68.       // =  Label op Panl zetten
  69.       lblTEXT:=TLabel.Create(self);
  70.       with lblTEXT do
  71.       begin
  72.         Parent := Pan1;
  73.         Left:= 33;
  74.         Top:=  23;
  75.         Font.Size:= 14;
  76.         Font.Name:= 'arial';
  77.         Font.Style:= [fsBold];
  78.         Font.Color:= clYellow;
  79.         Font.Style:= [fsBold];
  80.         Caption:= ' T E X T !';
  81.       end;
  82.       // = BUTTONJAPRINT op Panl zetten
  83.       btnJAPRINT:=TSpeedbutton.Create(self);
  84.       with btnJAPRINT do
  85.       begin
  86.         Parent := Pan1;
  87.         Caption := ' YES ';
  88.         Left := 30;
  89.         Top := 100;
  90.         Width := 100;
  91.         Height := 70;
  92.         Cursor:= crHandPoint;
  93.         font.Name:= 'arial';
  94.         font.Size:= 13;
  95.         font.Style:= [fsBold];
  96.         font.Color:= clRed;
  97.         // OnClick heeft een eigen proc. nodig.
  98.         OnClick := @btnTESTClick;
  99.       end;
  100.       // = BUTTONNEENPRINT op Panl zetten
  101.       btnNEENPRINT:=TSpeedbutton.Create(self);
  102.       with btnNEENPRINT do
  103.       begin
  104.         Parent := Pan1;
  105.         Caption := ' CLOSE ';
  106.         Left := 350;
  107.         Top := 100;
  108.         Width := 100;
  109.         Height := 70;
  110.         Cursor:= crHandPoint;
  111.         font.Name:= 'arial';
  112.         font.Size:= 13;
  113.         font.Style:= [fsBold];
  114.         font.Color:= clBlack;
  115.         // Bij klikken op de "btnNEENPRINT" wordt het Paneel verwijderd via de eigen proc.
  116.         // OnClick heeft een eigen proc. nodig.
  117.         OnClick:= @AnotherButtonClick;
  118.       end;
  119.     end;
  120.  
  121. end;
  122.  
  123. procedure TFmain.brnEXITClick(Sender: TObject);
  124. begin
  125.   Application.Terminate;
  126. end;
  127.  
  128. end.
  129.  

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: TPanel at runtime
« Reply #1 on: October 02, 2021, 01:37:40 pm »
Hi!

I think you confuse the compiler with the use of nested with

Close the with of the panel before you start the next with for the label.

Winni


howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TPanel at runtime
« Reply #2 on: October 02, 2021, 02:21:07 pm »
try this:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons, ExtCtrls;
  9.  
  10. type
  11.   TFmain = class(TForm)
  12.     btnShowPanel: TButton;
  13.     btnExit: TSpeedButton;
  14.     procedure btnExitClick(Sender: TObject);
  15.     procedure btnShowPanelClick(Sender: TObject);
  16.   private
  17.     Pan1: TPanel;
  18.     lblTEXT: TLabel;
  19.     btnJAPRINT: TSpeedButton;
  20.     btnNEENPRINT: TSpeedButton;
  21.     procedure AnotherButtonClick(Sender: TObject);
  22.     procedure btnTESTClick(Sender: TObject);
  23.   end;
  24.  
  25. var
  26.   Fmain: TFmain;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TFmain }
  33.  
  34. procedure TFmain.AnotherButtonClick(Sender: TObject);
  35. begin
  36.   ShowMessage('TFmain.AnotherButtonClick');
  37. end;
  38.  
  39. procedure TFmain.btnTESTClick(Sender: TObject);
  40. begin
  41.   ShowMessage('TFmain.btnTESTClick');
  42. end;
  43.  
  44. procedure TFmain.btnExitClick(Sender: TObject);
  45. begin
  46.   Close;
  47. end;
  48.  
  49. procedure TFmain.btnShowPanelClick(Sender: TObject);
  50. begin
  51.   Pan1 := TPanel.Create(Self);
  52.   with Pan1 do begin
  53.     Left:= 30;
  54.     Top := 50;
  55.     Width := 500;
  56.     Height := 250;
  57.     Color:= clYellow;
  58.     Caption:= ' Make your choice ';
  59.     Parent := Self;
  60.   end;
  61.  
  62.   lblTEXT := TLabel.Create(Self);
  63.   with lblTEXT do begin
  64.     Left:= 33;
  65.     Top:=  23;
  66.     Font.Size:= 14;
  67.     Font.Name:= 'arial';
  68.     Font.Style:= [fsBold];
  69.     Font.Color:= clYellow;
  70.     Font.Style:= [fsBold];
  71.     Caption:= ' T E X T !';
  72.     Parent := Pan1;
  73.   end;
  74.  
  75.   btnJAPRINT := TSpeedbutton.Create(Self);
  76.   with btnJAPRINT do begin
  77.     Caption := ' YES ';
  78.     Left := 30;
  79.     Top := 100;
  80.     Width := 100;
  81.     Height := 70;
  82.     Cursor:= crHandPoint;
  83.     font.Name:= 'arial';
  84.     font.Size:= 13;
  85.     font.Style:= [fsBold];
  86.     font.Color:= clRed;
  87.     OnClick  := @btnTESTClick;
  88.     Parent := Pan1;
  89.   end;
  90.  
  91.   btnNEENPRINT := TSpeedbutton.Create(Self);
  92.   with btnNEENPRINT do begin
  93.     Caption := ' CLOSE ';
  94.     Left := 350;
  95.     Top := 100;
  96.     Width := 100;
  97.     Height := 70;
  98.     Cursor:= crHandPoint;
  99.     font.Name:= 'arial';
  100.     font.Size:= 13;
  101.     font.Style:= [fsBold];
  102.     font.Color:= clBlack;
  103.     OnClick := @AnotherButtonClick;
  104.     Parent := Pan1;
  105.   end;
  106. end;
  107.  
  108. end.


jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: TPanel at runtime
« Reply #3 on: October 02, 2021, 03:00:49 pm »
Hi!

I think you confuse the compiler with the use of nested with

Close the with of the panel before you start the next with for the label.

Winni

It seems I remember back in the old Delphi days there was a way to obtain the Object pointer within a WITH statement of the current WITH so that it could be used also inside the Block.

 I would need to investigate how that was done.
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: TPanel at runtime
« Reply #4 on: October 03, 2021, 12:39:05 am »
I found my code frag I used in my Delphi code, Looking at it and seeing how fpc generates code I don't think it will work with fpc..

Have a good day.
The only true wisdom is knowing you know nothing

seghele0

  • Full Member
  • ***
  • Posts: 173
Re: TPanel at runtime
« Reply #5 on: October 03, 2021, 11:37:48 am »
howardpc, thank you for your code.
 ;)
But the label is still not shown on the 'Panel'.
With TStaticText it doesn't work either.
 :'(


DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: TPanel at runtime
« Reply #6 on: October 03, 2021, 12:28:34 pm »
Well.
The color of the panel is yellow + The color of the label is yellow = invisible !
Code: Pascal  [Select][+][-]
  1. Font.Color:= clRed;

seghele0

  • Full Member
  • ***
  • Posts: 173
Re: TPanel at runtime
« Reply #7 on: October 03, 2021, 01:03:37 pm »
Thank you so much.
Stupid, stupid of me.
At the age of 71, my eyes are slowing down.
Everything works after adjusting the color.
 :)

 

TinyPortal © 2005-2018