Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 17 total
Thread Removing Control
Thu, Jun 8 2017 8:47 AMPermanent Link

Chris Holland

SEC Solutions Ltd.

Avatar

Team Elevate Team Elevate

What is the correct way to remove an unwanted control from a form?

I have some TBasicPanels that are sub panels of a question panel and I
want to remove them so I did this:

  for i := QuestionsPanel.ControlCount - 1 downto 0 do
   begin
      c := QuestionsPanel.Controls[i];
      if c is TBasicPanel then
      begin
         c.Free;
      end;
   end;

But doing this when the form closes and I free the form throws an error.
Comment out the c.Free part of the code and the error disappears!

--
Chris Holland
[Team Elevate]
Thu, Jun 8 2017 9:16 AMPermanent Link

Chris Holland

SEC Solutions Ltd.

Avatar

Team Elevate Team Elevate

I found out the problem.

It didn't like it because I had declared c as TComponent instead of
TControl.

Changed that and it is all working now.

Chris Holland
[Team Elevate]

On 08/06/2017 13:47, Chris Holland wrote:
> What is the correct way to remove an unwanted control from a form?
>
> I have some TBasicPanels that are sub panels of a question panel and I
> want to remove them so I did this:
>
>   for i := QuestionsPanel.ControlCount - 1 downto 0 do
>    begin
>       c := QuestionsPanel.Controls[i];
>       if c is TBasicPanel then
>       begin
>          c.Free;
>       end;
>    end;
>
> But doing this when the form closes and I free the form throws an error.
> Comment out the c.Free part of the code and the error disappears!
>
Thu, Jun 8 2017 9:18 AMPermanent Link

Mark Brooks

Slikware

Avatar

I have a procedure that I use all over the place the clear out the contents of a TScrollPanel:

procedure MCBClearScrollContents(const AScrollPanel: TScrollPanel);
var
 I: integer;
 C: TControl;
begin
 with AScrollPanel do
   begin
     BeginUpdate;
     try
       for I := ControlCount - 1 downto 0 do
         begin
           C := Controls[I];
           C.Parent := nil;
           Controls[I].Free;
         end;
     finally
       EndUpdate;
     end;
   end;
end;

It seems to me the key difference is that I'm setting the parent to nil?

Hope this helps
Mark
Fri, Jun 9 2017 7:24 AMPermanent Link

Chris Holland

SEC Solutions Ltd.

Avatar

Team Elevate Team Elevate

Hi Mark,

Okay, that helped thanks.

It turns out that doing this:

  C := Controls[I];
  C.Free

causes it to crash when I close the form, but doing this works:

  Controls[I].Parent := nil;
  Controls[i].Free;

I have to nil the parent and use the Controls[i].Free in stead of the
C.Free or it still crashes!

Not sure what the difference is between

  C := Controls[I];
  C.Free

 and

  C := Controls[I];
  Controls[I].Free

but it doesn't like the first one!

Chris Holland
[Team Elevate]

On 08/06/2017 14:18, Mark Brooks wrote:
> I have a procedure that I use all over the place the clear out the contents of a TScrollPanel:
>
> procedure MCBClearScrollContents(const AScrollPanel: TScrollPanel);
> var
>    I: integer;
>    C: TControl;
> begin
>    with AScrollPanel do
>      begin
>        BeginUpdate;
>        try
>          for I := ControlCount - 1 downto 0 do
>            begin
>              C := Controls[I];
>              C.Parent := nil;
>              Controls[I].Free;
>            end;
>        finally
>          EndUpdate;
>        end;
>      end;
> end;
>
> It seems to me the key difference is that I'm setting the parent to nil?
>
> Hope this helps
> Mark
>
Fri, Jun 16 2017 1:00 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Chris,

<< It didn't like it because I had declared c as TComponent instead of TControl.

Changed that and it is all working now. >>

That shouldn't make any difference - the TControl is a TComponent descendant class.

How were the controls being created ?  Manually, or as part of the form load ?

Tim Young
Elevate Software
www.elevatesoft.com
Sun, Jun 18 2017 3:49 PMPermanent Link

Chris Holland

SEC Solutions Ltd.

Avatar

Team Elevate Team Elevate

Hi Tim,

They were all created manually.

Chris Holland
[Team Elevate]

On 16/06/2017 18:00, Tim Young [Elevate Software] wrote:
> Chris,
>
> << It didn't like it because I had declared c as TComponent instead of TControl.
>
> Changed that and it is all working now. >>
>
> That shouldn't make any difference - the TControl is a TComponent descendant class.
>
> How were the controls being created ?  Manually, or as part of the form load ?
>
> Tim Young
> Elevate Software
> www.elevatesoft.com
>
Mon, Jun 19 2017 1:46 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Chris,

<< They were all created manually. >>

I'm still not seeing any issues here.  This is the code I'm using:

procedure TForm1.Button2Click(Sender: TObject);
var
  I: Integer;
begin
  for I:=1 to 4 do
     TBasicPanel.Create(Self);
end;

And then your code for deletion:

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  c: TComponent;
begin
 for i := ControlCount - 1 downto 0 do
  begin
     c := Controls[i];
     if c is TBasicPanel then
     begin
        c.Free;
     end;
  end;
end;

I just want to make sure that I'm not missing something here...

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Jun 20 2017 4:28 AMPermanent Link

Matthew Jones

Tim Young [Elevate Software] wrote:

> I just want to make sure that I'm not missing something here...

The key is in the line "doing this when the form closes and I free the form throws an error" in my experience. Things get hairy when things are closing, as you can easily remove something that then gets referenced later in the chain. The Chrome debugger usually tells me what I did wrong quite quickly.

--

Matthew Jones
Tue, Jun 20 2017 12:59 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< The key is in the line "doing this when the form closes and I free the form throws an error" in my experience.>>

Yeah, but I'm doing that.  I'm running this in the browser debugger to ensure that I see any shutdown errors that, typically, a lot of browsers suppress (but show in the console) because it just wants an orderly shutdown.

Tim Young
Elevate Software
www.elevatesoft.com
Wed, Jun 28 2017 3:35 AMPermanent Link

Mark Brooks

Slikware

Avatar

Mark Brooks wrote:

I have a procedure that I use all over the place the clear out the contents of a TScrollPanel:

procedure MCBClearScrollContents(const AScrollPanel: TScrollPanel);
var
 I: integer;
 C: TControl;
begin
 with AScrollPanel do
   begin
     BeginUpdate;
     try
       for I := ControlCount - 1 downto 0 do
         begin
           C := Controls[I];
           C.Parent := nil;
           Controls[I].Free;
         end;
     finally
       EndUpdate;
     end;
   end;
end;

It seems to me the key difference is that I'm setting the parent to nil?

Hope this helps
Mark

Tim - is this procedure ok - do I need to set the Parent to nil - thanks
Page 1 of 2Next Page »
Jump to Page:  1 2
Image