Hello, you may recall my previous email that I stated that I get a lot of 
access violation errors in a TListView when a pagecontrol's multiline 
property is changed. Well here's a test to prove the bug and the solution to 
work around it.

The bug

Add a TPageControl on a form and insert a few pages. Add a TListView inside 
one of the pages. Add 3 buttons to the form. Add a OnDeletion event to the 
TListView. Use this code for the test.

TtestObj = class(TObject)
end;

Implementation

procedure someOnClickProcedure1(Sender: TObject);
var
  i: integer;
begin
  for i:= 1 to 10 do
    ListView1.AddItem(Format('test%d', [i]), TtestObj.Create);
end;

procedure someOnClickProcedure2(Sender: TObject);
begin
  PageControl1.Multiline:= not PageControl1.Multiline;
end;

procedure someOnClickProcedure3(Sender: TObject);
begin
  ListView1.Clear;
end;

procedure ListView1Deletion(Sender: TObject; Item: TListItem);
begin
  TtestObj(Item.Data).Free;
end;

Ok by looking at this code, there is no problem right? The OnDeletion code 
clears up the memory that was allocated with the AddItem function. No memory 
leaks, every thing is fine. However, setting the Multiline property, when 
the TListView is inside the TPageControl, some how activates the OnDeletion 
event. This is the bug, it shouldn't do that, it's not logical. Now to fix 
this problem, we simply need a flag to prevent an unlawful deletion. Add a 
global or local variable called isDeleting of type boolean. Now we simply 
adjust the code like this

procedure someOnClickProcedure3(Sender: TObject);
begin
  isDeleting:= True;
  ListView1.Clear;
  isDeleting:= False;
end;

procedure ListView1Deletion(Sender: TObject; Item: TListItem);
begin
  if isDeleting then
    TtestObj(Item.Data).Free;
end;

voila! problem solved. 
__________________________________________________
Delphi-Talk mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi-talk

Reply via email to