(Using: Delphi XE)
I am adding a TButton to every row of a ListView. In the buttons OnClick handler is a Sender.Free. However (while the list row disappears because the dataset that populates the listview is updated,) the button remains on the listview when it should disappear. What am I doing wrong?
Here is my code that shows the creation of the button, and, the OnClick where it is to be freed:
(On another note, I know that its not good practice to destroy a component in its event handler. Is that what is wrong here? Can you suggest another method to remove the button?)
procedure TfMain.actWaitListExecute(Sender: TObject);
var
li: TListItem;
s: string;
btRect: TRect;
p: PInteger;
begin
lstWaitList.Items.Clear;
lstWaitList.Clear;
with uqWaitList do
begin
if State = dsInactive then
Open
else
Refresh;
First;
while not EOF do
begin
li := lstWaitList.Items.Add;
s := MyDateFormat(FieldByName('VisitDate').AsString);
li.Caption := s;
New(p);
p^ := FieldByName('ROWID').AsInteger;
li.Data := p;
s := MyTimeFormat(FieldByName('InTime').AsString);
li.SubItems.Add(s);
li.SubItems.Add(FieldByName('FirstName').AsString + ' ' +
FieldByName('LastName').AsString);
// li.SubItems.Add(FieldByName('LastName').AsString);
with TButton.Create(lstWaitList) do
begin
Parent := lstWaitList;
btRect := li.DisplayRect(drBounds);
btRect.Left := btRect.Left + lstWaitList.Column[0].Width +
lstWaitList.Column[1].Width + lstWaitList.Column[2].Width;
btRect.Right := btRect.Left + lstWaitList.Column[3].Width;
BoundsRect := btRect;
Caption := 'Check Out';
OnClick := WaitingListCheckOutBtnClick;
end;
Next;
end;
end;
end;
procedure TfMain.lstWaitListDeletion(Sender: TObject; Item: TListItem);
begin
Dispose(Item.Data);
end;
procedure TfMain.WaitingListCheckOutBtnClick(Sender: TObject);
var
SelROWID, outtime: integer;
x: longword;
y: TPoint;
h, mm, s, ms: word;
begin
y := lstWaitList.ScreenToClient(Mouse.CursorPos);
// Label23.Caption := Format('%d %d', [y.X, y.y]);
x := (y.y shl 16) + y.X;
PostMessage(lstWaitList.Handle, WM_LBUTTONDOWN, 0, x);
PostMessage(lstWaitList.Handle, WM_LBUTTONUP, 0, x);
Application.ProcessMessages;
SelROWID := integer(lstWaitList.Selected.Data^);
// ShowMessage(IntToStr(SelROWID));
with TfCheckOut.Create(Application) do
begin
try
if ShowModal = mrOk then
begin
decodetime(teTimeOut.Time, h, mm, s, ms);
outtime := h * 100 + mm;
uqSetOutTime.ParamByName('ROWID').Value := SelROWID;
uqSetOutTime.ParamByName('OT').Value := outtime;
uqSetOutTime.Prepare;
uqSetOutTime.ExecSQL;
(TButton(Sender)).Visible := False;
(TButton(Sender)).Free;
actWaitListExecute(Self);
end;
finally
Free;
end;
end;
end;
Image: