URGENT: Why the content of painbox get losted ?

Hi,
I draw something on paintbox but if i go to another form and come back to that form again drawed paint was get losted?
Why? How can i avoid from this problem?

Regards
shirinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russell LibbySoftware Engineer, Advisory Commented:
The paintbox is not a static canvas, thus you need to perform your actions in the Paint procedure of the TPaintBox contol. Btw, if you are painting something static, you might try drawing on a bitmap, and then assigning the bitmap to a TImage control instead.

Regards,
Russell

0
shirinAuthor Commented:
I dont want to static this Figure.
I only Need My program Change this figure.
with  overlapping another form with  paintbox form
previous Painted figure was get losted.

0
shirinAuthor Commented:
OK,
with Your Guidance I found:TImage is Better For my purpose.

But i can not clear previous paint when i click a button.

I added Extra points for my last  Q.



0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Russell LibbySoftware Engineer, Advisory Commented:
You can also use the offscreen bitmap to perform updates on, then have the paintbox draw the bitmap on its canvas. Example provided below (just add you painting code to the UpdateBitmap procedure):

Regards,
Russell

----

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

type
  TForm1            =  class(TForm)
     PaintBox1:     TPaintBox;
     Button1:       TButton;
     procedure      PaintBox1Paint(Sender: TObject);
     procedure      FormCreate(Sender: TObject);
     procedure      FormDestroy(Sender: TObject);
     procedure      Button1Click(Sender: TObject);
  private
     // Private declarations
     FBitmap:       TBitmap;
  protected
     // Protected declarations
     procedure      UpdateBitmap;
  public
     // Public declarations
  end;

var
  Form1:            TForm1;

implementation
{$R *.DFM}

procedure TForm1.UpdateBitmap;
begin

  // Update the bitmap with something
  with FBitmap.Canvas do
  begin
     FillRect(Rect(0, 0, FBitmap.Width, FBitmap.Height));
     TextOut(10, 10, 'Hello world');
  end;

  // Force paintbox to repaint
  PaintBox1.Repaint;

end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin

  // Draw the static bitmap on the paintbox control canvas
  PaintBox1.Canvas.Draw(0, 0, FBitmap);

end;

procedure TForm1.FormCreate(Sender: TObject);
begin

  // Create bitmap
  FBitmap:=TBitmap.Create;

  // Set bounds
  FBitmap.Width:=PaintBox1.Width;
  FBitmap.Height:=PaintBox1.Height;

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin

  // Free the bitmap
  FBitmap.Free;
 
end;

procedure TForm1.Button1Click(Sender: TObject);
begin

  // Example of updating the bitmap
  UpdateBitmap;

end;

end.

----------

object Form1: TForm1
  Left = 232
  Top = 114
  Width = 475
  Height = 343
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object PaintBox1: TPaintBox
    Left = 104
    Top = 16
    Width = 257
    Height = 233
    OnPaint = PaintBox1Paint
  end
  object Button1: TButton
    Left = 16
    Top = 16
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
end


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shirinAuthor Commented:
Hi, How can I clear a Previous pain and paint a new one dynamically.
0
shirinAuthor Commented:
Ok,
I can it with
image1.picture.bitmap:=nil;
and draw another figure.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.