tags:

views:

130

answers:

1

Hi,

I have 2 forms. Form1 with one panel and 2 buttons (pnl1, btnShowTree and btnAddItems). There is also Form2 which contains Treeview (tv1).

Please see short code below to understand this little demonstration:

procedure TForm1.btnShowTreeClick(Sender: TObject);
begin
   with Form2 do
   begin
   tv1.Items.clear;
   Tv1.Items.AddChild(nil, '1.' );
   Tv1.Items.AddChild(nil, '2.' );
   Tv1.Items.AddChild(nil, '3.' );
   Form2.Parent:=pnl1;
   Form2.BorderStyle:=bsNone;
   Form2.show;
   end;
end;

procedure TForm1.btnAddItemsClick(Sender: TObject);
begin
 with Form2 do
   begin
   BorderStyle:=bsSizeable;  // here it works wrong
   tv1.Items.clear;
   Tv1.Items.AddChild(nil, 'A.' );
   Tv1.Items.AddChild(nil, 'B.' );
   Tv1.Items.AddChild(nil, 'C.' );
 //  BorderStyle:=bsSizeable;  here it works fine. WHY ?????  
   Form2.Show;
   end;
end;

procedure TForm2.btnCloseForm2Click(Sender: TObject);
begin
Parent:=nil;   
Hide;          
// when I exchange instructions order  like:
// Hide;
// Parent:=nil;
// I get the same problem  with improperly nested BorderStyle:=bsSizeable; I have 
// only blur idea why it is so...
end;

I expected, that when I click on btnAddItems I will see 3 items (A. B. C.). But it will show 6 items, because the previous ones are not deleted !!! can anybody thow a light on it, 'cause I stucked here for hours to make program work well, but I still have not the thiniest idea what I do wrong...

+2  A: 

Changing the BorderStyle at runtime means that the window has to be destroyed and recreated. This means that the VCL has to store the content of whatever controls are on the form (like your TTreeView), destroy the form, create the form with the new BorderStyle, recreate all the controls on the form, and then restore all the content.

You're probably using an older version of Delphi (see note below) that doesn't properly remove the stored content from memory. @M Schenkel is using a later version that does.

The solution, of course, is to stop changing the BorderStyle at runtime, which will stop causing the form to be destroyed and recreated. :-) I've been programming with Delphi starting with version 1 and continuing through the current Delphi 2010, and in all that time I have never once had a need to change BorderStyle at runtime.

NOTE: When posting a Delphi question, you should always indicate what version of Delphi you're using. Differences in Delphi version means differences in the VCL, and a problem can be caused by different things in those different versions. Knowing what version of Delphi you're using makes solving your problem or answering your question easier.

Ken White
thanks a lot... getting smarter slowly ;-)I am using Delphi 7...
lyborko