Re: OpenPictureDialog Problems



<cybatech@xxxxxxxxxxxxx> .wrote..
> Sorry for being so vague. When I run it I get Acces Violation.

Here is your first problem

You have

procedure TForm1.SpeedButton2Click(Sender: TObject);
var
NewShape : TManyShape;
begin
NewShape := TManyShape.Create(self);

Here NewShape is a local variable and has meaning only within the
SpeedButton2Click method.

When the SpeedButton2Click method concludes "NewShape" ceases to have
any meaning and is lost in the wide blue yonder.

And so all you are left with is a memory leak.

And in the Button1Click method you declare another local variable
named NewShape which only has meaning within *this* method,

procedure TForm1.Button1Click(Sender: TObject);
var OpenPicDlg : TOpenPictureDialog;
Bitmap: TBitmap;
NewShape : TManyShape;

If you want a variable to be visible within more than one method,
declare it as a private class variable (e.g. fNewShape).

But you are creating a whole lot of shapes aren't you? Then you can't
keep using the same name. The simplest way is to use a list or an
array of tManyShapes and add the shapes as you create them.

In the TForm1.Button1Click method use the Sender parameter so that
your program knows which shape has been clicked on

procedure TForm1.Button1Click(Sender: TObject);
var OpenPicDlg : TOpenPictureDialog;
// Bitmap: TBitmap; no need for this line, it just confuses things
NewShape : TManyShape; // NewShape is local alias
begin
NewShape := Sender as TManyShape;
OpenPicDlg := TOpenPictureDialog.Create(Self);
Try
if OpenPicDlg.Execute then
NewShape.Image.Picture.Bitmap.LoadFromFile(OpenPicDlg.FileName);
// watch the wrap and see note * below
finally
OpenPicDlg.Free
end;
end;

end

* Note

Your lines

bitmap.LoadFromFile(OpenPicDlg.FileName);
Newshape.Image :=Bitmap;

really confuse me

What is the type of the objects you are loading from file?

>From your code it looks as though they may be tBitmaps.

I am not familiar with TManyShape but it sounds as though it is a
descendant of the TShape.component.

Does TManyShape have a property named Image?

If it does then I suspect that it is of type TImage.

If this is so then tManyShape.Image is not compatible with tBitmap.

But tImage.Picture does have a Bitmap property.

So your line Newshape.Image := Bitmap should probably have read

Newshape.Image.Picture.Bitmap := Bitmap.

HTH

--
Henry Bartlett
Delphi Links Page:
( http://www.hotkey.net.au/~hambar/habit/delflink.htm )






.


Quantcast