Recent

Author Topic: [SOLVED] Manually adding user generated Controls  (Read 1845 times)

Nitorami

  • Sr. Member
  • ****
  • Posts: 496
[SOLVED] Manually adding user generated Controls
« on: October 01, 2017, 06:23:44 pm »
I am trying to add a paintbox to Form1 per Menu Click but it does not work. Nothing shows up, and I am feeling really stupid here.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.MenuItem1Click(Sender: TObject);
  2. begin
  3.    try
  4.      TestBox := TPaintbox.Create(Self);
  5.      TestBox.Parent:=self;
  6.      Label1.Caption := 'TestBox generated...'
  7.      with TestBox do begin
  8.        Left := 10; Top  := 10;  Width := 50; Height := 50;
  9.        Canvas.Brush.Style := bsSolid;
  10.        Canvas.Brush.Color := clblue;
  11.        Canvas.Rectangle(5,5,8,8);
  12.        Canvas.FillRect(0,0,10,10);
  13.      end;
  14.    finally
  15.    end;
  16. end;    
« Last Edit: October 03, 2017, 10:12:23 pm by Nitorami »

bytebites

  • Hero Member
  • *****
  • Posts: 640
Re: Manually adding user generated Controls
« Reply #1 on: October 01, 2017, 06:34:26 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.      try
  4.      TestBox := TPaintbox.Create(Self);
  5.      TestBox.Parent:=self;
  6.      Label1.Caption := 'TestBox generated...';
  7.      with TestBox do begin
  8.        Left := 10; Top  := 10;  Width := 50; Height := 50;
  9.        OnPaint:=PaintBox1paint;
  10.      end;
  11.    finally
  12.    end;
  13. end;
  14.  
  15. procedure TForm1.PaintBox1Paint(Sender: TObject);
  16. begin
  17.    Canvas.Brush.Style := bsSolid;
  18.    Canvas.Brush.Color := clblue;
  19.    Canvas.Rectangle(5,5,8,8);
  20.    Canvas.FillRect(0,0,10,10);
  21. end;
  22.    

Nitorami

  • Sr. Member
  • ****
  • Posts: 496
Re: Manually adding user generated Controls
« Reply #2 on: October 01, 2017, 06:46:09 pm »
Ah, I see, I need to specify an OnPaint Event. Thank you.

Edit: Wait. The PaintBox1Paint executes, but I still cannot see anything.

Aha, it should say
Code: Pascal  [Select][+][-]
  1. procedure TForm1.PaintBox1Paint(Sender: TObject);
  2. begin
  3.   with TPaintBox(Sender) do begin
  4.    Canvas.Brush.Style := bsSolid;
  5.    Canvas.Brush.Color := clblue;
  6.    Canvas.Rectangle(5,5,8,8);
  7.    Canvas.FillRect(0,0,10,10);
  8. end;
  9. end;

« Last Edit: October 01, 2017, 07:04:49 pm by Nitorami »

 

TinyPortal © 2005-2018