Create items in a listview dynamically at runtime with delphi firemonkey - Stack Overflow

With the code below I can create a button inside a listview item but when I click on the button it caus

With the code below I can create a button inside a listview item but when I click on the button it causes an error, any ideas to solve it, apparently it could be because I am not able to add a relative to the button.

procedure TForm1.btAddClick(Sender: TObject);
var
  item : TListItemText;
  AItem:  TListViewItem;
begin
  AItem := ListView1.Items.AddItem;

end;

procedure TForm1.ListView1UpdateObjects(const Sender: TObject; const AItem: TListViewItem);
begin
  if AItem.Objects.FindObject('MyButtonCustom') = nil then
  begin
    Create_BT(AItem);
  end;
end;

procedure TForm1.Create_BT(const AItem: TListViewItem);
var
  LItem: TListItemTextButton;
begin
  LItem                       := TListItemTextButton.Create(nil);
  AItem.BeginUpdate;

  LItem.Name                  := 'MyButtonCustom';
  LItem.Text                  := 'MyButtonCustom';

  LItem.Height                := 20;
  LItem.Width                 := 200;
  LItem.PlaceOffset.X         := 0;
  LItem.PlaceOffset.X         := 0;

  LItem.Align                 := TListItemAlign.Center;
  LItem.VertAlign             := TListItemAlign.Center;

  LItem.TextAlign             := TTextAlign.Center;
  LItem.TextVertAlign         := TTextAlign.Center;

  AItem.EndUpdate;

  AItem.Objects.Add(LItem);
end;

This is the error that I receive (copied from comments by @tobya)

LItem := TListItemTextButton.Create(nil)

When clicking on the button that was created, the following errors appear:


raised exception class $C0000005 with message 'access violation at 0x00a85323: read of address 0x00000000.' Access violation at address 00A85323 in module 'Project1.exe' (offset 575323). Read of address 00000000. LItem := TListItemTextButton.Create(AItem); 

closing the form causes the errors:

Project Project.exe raised exception class EInvalidPointer with message 'Invalid point operation'. Exception EInvalidPointer in module Project1.exe at 000085CD Invalid pointer operation.

With the code below I can create a button inside a listview item but when I click on the button it causes an error, any ideas to solve it, apparently it could be because I am not able to add a relative to the button.

procedure TForm1.btAddClick(Sender: TObject);
var
  item : TListItemText;
  AItem:  TListViewItem;
begin
  AItem := ListView1.Items.AddItem;

end;

procedure TForm1.ListView1UpdateObjects(const Sender: TObject; const AItem: TListViewItem);
begin
  if AItem.Objects.FindObject('MyButtonCustom') = nil then
  begin
    Create_BT(AItem);
  end;
end;

procedure TForm1.Create_BT(const AItem: TListViewItem);
var
  LItem: TListItemTextButton;
begin
  LItem                       := TListItemTextButton.Create(nil);
  AItem.BeginUpdate;

  LItem.Name                  := 'MyButtonCustom';
  LItem.Text                  := 'MyButtonCustom';

  LItem.Height                := 20;
  LItem.Width                 := 200;
  LItem.PlaceOffset.X         := 0;
  LItem.PlaceOffset.X         := 0;

  LItem.Align                 := TListItemAlign.Center;
  LItem.VertAlign             := TListItemAlign.Center;

  LItem.TextAlign             := TTextAlign.Center;
  LItem.TextVertAlign         := TTextAlign.Center;

  AItem.EndUpdate;

  AItem.Objects.Add(LItem);
end;

This is the error that I receive (copied from comments by @tobya)

LItem := TListItemTextButton.Create(nil)

When clicking on the button that was created, the following errors appear:


raised exception class $C0000005 with message 'access violation at 0x00a85323: read of address 0x00000000.' Access violation at address 00A85323 in module 'Project1.exe' (offset 575323). Read of address 00000000. LItem := TListItemTextButton.Create(AItem); 

closing the form causes the errors:

Project Project.exe raised exception class EInvalidPointer with message 'Invalid point operation'. Exception EInvalidPointer in module Project1.exe at 000085CD Invalid pointer operation.
Share edited Mar 20 at 7:11 Toby Allen 11.3k12 gold badges79 silver badges131 bronze badges asked Mar 8 at 23:01 maugustomorenomaugustomoreno 212 bronze badges 6
  • 1 Each control needs both an owner and a parent. I'm not sure what AItem.Objects.Add() does, but I suspect that it might add a parent but not an owner. Have you tried LItem:=TListItemTextButton.Create(AItem)? – Philip J. Rayment Commented Mar 9 at 3:48
  • yes, thanks for commenting. when I do it like this Item := TListItemText Button.Create(AItem); when closing the application it causes memory errors memoryleaks – maugustomoreno Commented Mar 9 at 11:24
  • You say you get an error, but what IS the error you get? – Eirik A. Commented Mar 9 at 11:57
  • How can I explain it to you, memory leak error, I added the command ReportMemoryLeaksOnShutdown := DebugHook <> 0; in the project's .dpr file and it reports a very large memory error. Project Project1.exe raised exception class EInvalidPointer with message 'Invalid pointer operation'. – maugustomoreno Commented Mar 9 at 13:30
  • @maugustomoreno You can copy the error message and paste it into your post (or to a comment). – Tom Brunberg Commented Mar 9 at 17:56
 |  Show 1 more comment

1 Answer 1

Reset to default 0

Well, I slightly changed your code for testing and this works (D12.2)

procedure TForm1.Button1Click(Sender: TObject);
var aItem : TListViewItem;
begin
  ListView1.BeginUpdate;
  aItem := ListView1.Items.Add;
  aItem.Text := 'hello '+ListView1.Items.Count.tostring;
  ListView1.EndUpdate;
end;

procedure TForm1.ListView1UpdateObjects(const Sender: TObject;
  const AItem: TListViewItem);
procedure Create_BT(const AItem: TListViewItem);
var
  LItem: TListItemTextButton;
begin
  LItem                       := TListItemTextButton.Create(nil);
//  AItem.BeginUpdate;

  LItem.Name                  := 'MyButtonCustom';
  LItem.Text                  := 'MyButtonCustom';

  LItem.Height                := 20;
  LItem.Width                 := 200;
  LItem.PlaceOffset.X         := 0;
  LItem.PlaceOffset.X         := 0;

  LItem.Align                 := TListItemAlign.Center;
  LItem.VertAlign             := TListItemAlign.Center;

  LItem.TextAlign             := TTextAlign.Center;
  LItem.TextVertAlign         := TTextAlign.Center;

//  AItem.EndUpdate;

  AItem.Objects.Add(LItem);

end;
begin
  if AItem.Objects.FindObject('MyButtonCustom') = nil then
  begin
    Create_BT(AItem);
  end;
end;

Following my comment and using a dynamic appearance it's easy

procedure TForm1.Button1Click(Sender: TObject);
var aItem : TListViewItem;
begin
  ListView1.BeginUpdate;
  aItem := ListView1.Items.Add;
  var aLabel:=aItem.Objects.FindObjectT<TListItemText>('Text1'); // case sensitive !!
  if assigned(alabel) then TListItemText(aLabel).text := 'hello '+ListView1.Items.Count.tostring;
  ListView1.EndUpdate;
end;

procedure TForm1.ListView1ItemClickEx(const Sender: TObject; ItemIndex: Integer;
  const LocalClickPos: TPointF; const ItemObject: TListItemDrawable);
begin
Showmessage(ItemObject.Name);
end;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744883409a4598975.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信