Board index » delphi » Displaying a shape created at runtime on a form

Displaying a shape created at runtime on a form

I'm trying to get a shape declared as part of an object displayed on a
form:

type TPerson = class
     [...]
     Male : Boolean;
     PersonShape : Tshape;
     [...]
end;
[...]
var Someone : TPerson;
[...]
procedure TForm1.FormCreate(Sender: TObject);
begin
   Someone := TPerson.Create;
   Someone.Male := True;

Up to here all is well - my object exists and I can get at the fields,
but when I try to get at the TShape field in order to make it show up
on the form...

   Someone.Personshape.Top := 100;
end;

...it falls over (inscrutable access violation). Can anyone help?

--
Adrian Smith

Sent via Deja.com
http://www.deja.com/

 

Re:Displaying a shape created at runtime on a form


Hi Adrian!

Quote
> procedure TForm1.FormCreate(Sender: TObject);
> begin
>    Someone := TPerson.Create;
>    Someone.Male := True;

PersonShape := TShape.Create; ...

--> you have to create the instance of TShape aswell! Otherwise PersonShape
= NIL --> "acces violation ... trying to read from adress $FFFFFFFFF" or
something...

Bye
  Tobi

Re:Displaying a shape created at runtime on a form


In article <959s8f$gg0o...@ID-38935.news.dfncis.de>,
  "Tobias Hermann" <to...@mayn.de> wrote:

Quote
> Hi Adrian!

Hi! And thanks.

Quote
> > procedure TForm1.FormCreate(Sender: TObject);
> > begin
> >    Someone := TPerson.Create;
> >    Someone.Male := True;
> PersonShape := TShape.Create; ...

> --> you have to create the instance of TShape aswell! Otherwise
> PersonShape = NIL --> "acces violation ... trying to read from adress
> $FFFFFFFFF" or something...

Hmm. OK, "Someone.PersonShape := TShape.Create(Form1);" seems to
compile and run happily enough. But I still can't get the shape to show
up on the form!

--
Adrian Smith

Sent via Deja.com
http://www.deja.com/

Re:Displaying a shape created at runtime on a form


Quote
"Adrian Smith" <adrian_p_sm...@yahoo.com> wrote in message

news:95a3sa$7c9$1@nnrp1.deja.com...

Quote
> In article <959s8f$gg0o...@ID-38935.news.dfncis.de>,
>   "Tobias Hermann" <to...@mayn.de> wrote:
> > Hi Adrian!

> Hi! And thanks.

> > > procedure TForm1.FormCreate(Sender: TObject);
> > > begin
> > >    Someone := TPerson.Create;
> > >    Someone.Male := True;
> > PersonShape := TShape.Create; ...

> > --> you have to create the instance of TShape aswell! Otherwise
> > PersonShape = NIL --> "acces violation ... trying to read from adress
> > $FFFFFFFFF" or something...

> Hmm. OK, "Someone.PersonShape := TShape.Create(Form1);" seems to
> compile and run happily enough. But I still can't get the shape to show
> up on the form!

PersonShape := tShape.Create (Self);
PersonShape.Parent := Self;
PersonShape.SetBounds (theLeftPos, theTopPos, theWidth, theHeight);

Re:Displaying a shape created at runtime on a form


Quote

> PersonShape := tShape.Create (Self);
> PersonShape.Parent := Self;
> PersonShape.SetBounds (theLeftPos, theTopPos, theWidth, theHeight);

And perhaps InsertControl(PersonShape);

Then you don't have to call PersonShape.Free when closing/destroing your
form...

Tobi

Re:Displaying a shape created at runtime on a form


You don't need to call InsertControl, because you already supplied Self as
owner.

PersonShape := TShape.Create(Self);

is identical to

PersonShape := TShape.Create(nil);
Self.InsertControl(PersonShape);

"Tobias Hermann" <to...@mayn.de> schreef in bericht
news:95b5h4$gi81t$1@ID-38935.news.dfncis.de...

Quote

> > PersonShape := tShape.Create (Self);
> > PersonShape.Parent := Self;
> > PersonShape.SetBounds (theLeftPos, theTopPos, theWidth, theHeight);

> And perhaps InsertControl(PersonShape);

> Then you don't have to call PersonShape.Free when closing/destroing your
> form...

> Tobi

Re:Displaying a shape created at runtime on a form


Quote
"Tobias Hermann" <to...@mayn.de> wrote in message

news:95b5h4$gi81t$1@ID-38935.news.dfncis.de...

Quote

> > PersonShape := tShape.Create (Self);
> > PersonShape.Parent := Self;
> > PersonShape.SetBounds (theLeftPos, theTopPos, theWidth, theHeight);

> And perhaps InsertControl(PersonShape);

> Then you don't have to call PersonShape.Free when closing/destroing your
> form...

As long as PersonShape has a non-nil Owner &/or Parent property, it will be
destroyed automatically.

Re:Displaying a shape created at runtime on a form


Quote
> PersonShape := TShape.Create(Self);

> is identical to

> PersonShape := TShape.Create(nil);
> Self.InsertControl(PersonShape);

Are you really sure? I remember having read something in my delphi book that
a component can have another owner than in whose control list it stands.

So  you can make the owner e.g. a scrollbox but insert it in the control
list of the formular ...

And: If I only do something like
  MyControl := TMyControl.Create(self); self is the formular; tmycontrol is
a descendant class from tcustomcontrol

I get error exceptions.

If I add
  InsertControl(MyControl) everything is fine!

What do you think?

Tobi

Re:Displaying a shape created at runtime on a form


There are only 2 lists: Components and Controls. You can put a component in
the Components list by setting the Owner. You can put a component in the
Controls list by setting the Parent.
If either the Owner or the Parent is destroyed, the component will also be
destroyed.

"Tobias Hermann" <to...@mayn.de> schreef in bericht
news:95e6m6$gur8l$1@ID-38935.news.dfncis.de...

Quote
> > PersonShape := TShape.Create(Self);

> > is identical to

> > PersonShape := TShape.Create(nil);
> > Self.InsertControl(PersonShape);

> Are you really sure? I remember having read something in my delphi book
that
> a component can have another owner than in whose control list it stands.

> So  you can make the owner e.g. a scrollbox but insert it in the control
> list of the formular ...

> And: If I only do something like
>   MyControl := TMyControl.Create(self); self is the formular; tmycontrol
is
> a descendant class from tcustomcontrol

> I get error exceptions.

> If I add
>   InsertControl(MyControl) everything is fine!

> What do you think?

> Tobi

Re:Displaying a shape created at runtime on a form


In article <95e917$8q...@porthos.nl.uu.net>, "M.H. Avegaart"

Quote
<avegaartNOS...@mccomm.nl> writes:
>If either the Owner or the Parent is destroyed, the component will also be
>destroyed.

Is that correct, I thought the Owner allocated and freed the memory, and
destroyed the component; and that the Parent was the windows' message router to
the child.

So without an Owner the coder had to free the component, and without a Parent
then it did not even show itself (as the WM_SHOWWINDOW message was not sent on
to the component).

Alan Lloyd
alangll...@aol.com

Re:Displaying a shape created at runtime on a form


On 02 Feb 2001 16:19:37 GMT, alangll...@aol.com (AlanGLLoyd) wrote:

Quote
>In article <95e917$8q...@porthos.nl.uu.net>, "M.H. Avegaart"
><avegaartNOS...@mccomm.nl> writes:

>>If either the Owner or the Parent is destroyed, the component will also be
>>destroyed.

>Is that correct, I thought the Owner allocated and freed the memory, and
>destroyed the component; and that the Parent was the windows' message router to
>the child.

>So without an Owner the coder had to free the component, and without a Parent
>then it did not even show itself (as the WM_SHOWWINDOW message was not sent on
>to the component).

You could look it up. It's not hard to find, only took me a minute
or so. In D3:

destructor TWinControl.Destroy;
var
  I: Integer;
  Instance: TControl;
begin
[...]
  I := ControlCount;
  while I <> 0 do
  begin
    Instance := Controls[I - 1];
    Remove(Instance);
    Instance.Destroy;
    I := ControlCount;
  end;
[...]
end;

destructor TComponent.Destroy;
[...]
  DestroyComponents;
[...]
end;

procedure TComponent.DestroyComponents;
var
  Instance: TComponent;
begin
  while FComponents <> nil do
  begin
    Instance := FComponents.Last;
    Remove(Instance);
    Instance.Destroy;
  end;
end;

Quote
>Alan Lloyd
>alangll...@aol.com

Re:Displaying a shape created at runtime on a form


In article <3a7b3da7.4117...@nntp.sprynet.com>, ullr...@math.okstate.edu (David

Quote
C. Ullrich) writes:
>You could look it up. It's not hard to find, only took me a minute
>or so. In D3:

Which filename is that in ?

Alan Lloyd
alangll...@aol.com

Re:Displaying a shape created at runtime on a form


On 04 Feb 2001 07:39:09 GMT, alangll...@aol.com (AlanGLLoyd) wrote:

Quote
>In article <3a7b3da7.4117...@nntp.sprynet.com>, ullr...@math.okstate.edu (David
>C. Ullrich) writes:

>>You could look it up. It's not hard to find, only took me a minute
>>or so. In D3:

>Which filename is that in ?

In the spirit of that aphorism about giving a man a fish versus
teaching him to fish I should say this: There's a Find utility
included in Windows Explorer: right-click on a directory,
sorry, on a folder and hit Find. You can use it to search
for files containing a certain string (you have to go to the
Advanced page in the dialog in Windows 95, while it's on
the front page in Windows 98.)

There's only one file in Delphi 3\Source that contains the
string "TWinControl.Destroy", also only one that contains
the string "TComponent.Destroy"

Generally searching for "TTypeName.MethodName" will
give only one hit.

Quote
>Alan Lloyd
>alangll...@aol.com

Re:Displaying a shape created at runtime on a form


"David C. Ullrich" <ullr...@math.okstate.edu> wrote in message
news:3a7d5c0a.3904297@nntp.sprynet.com...

Quote
> teaching him to fish I should say this: There's a Find utility
> included in Windows Explorer: right-click on a directory,
> sorry, on a folder and hit Find. You can use it to search
> for files containing a certain string (you have to go to the
> Advanced page in the dialog in Windows 95, while it's on
> the front page in Windows 98.)

I tend to use Search | Find in Files in the Delphi IDE.

Re:Displaying a shape created at runtime on a form


Quote
Bruce Roberts wrote:

> "David C. Ullrich" <ullr...@math.okstate.edu> wrote in message
> news:3a7d5c0a.3904297@nntp.sprynet.com...

> > teaching him to fish I should say this: There's a Find utility
> > included in Windows Explorer: right-click on a directory,
> > sorry, on a folder and hit Find. You can use it to search
> > for files containing a certain string (you have to go to the
> > Advanced page in the dialog in Windows 95, while it's on
> > the front page in Windows 98.)

> I tend to use Search | Find in Files in the Delphi IDE.

That works too (if you have Delphi running - I more or less
always have an Explorer window open but Delphi takes a little
time to start.)

Other Threads