Recent

Author Topic: Adding a button to certain items of TListView  (Read 2633 times)

blankname

  • New Member
  • *
  • Posts: 31
Adding a button to certain items of TListView
« on: January 03, 2022, 03:11:32 pm »
Is there any way of adding a button to certain items of TListView? I'd like to add one or two buttons to items with specific data.
I've been searching on the internet, and found that Delphi 10.1 offers such thing with ItemAppearance, but Lazarus does not have it...

wp

  • Hero Member
  • *****
  • Posts: 9583
Re: Adding a button to certain items of TListView
« Reply #1 on: January 03, 2022, 04:21:04 pm »
I don't know of this possibility (but of course, this does not mean anything...).

I'd switch to a TStringGrid which can be set up to look very similar to a TListView in report mode. Or I'd use TVirtualTreeView which can be set to a grid/listview-like appearance - this will require some major changes within your application though.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

GetMem

  • Hero Member
  • *****
  • Posts: 3741
Re: Adding a button to certain items of TListView
« Reply #2 on: January 03, 2022, 04:50:53 pm »
What is ViewStyle? vsReport?  If yes, then you can try something like this:
Code: Pascal  [Select][+][-]
  1. uses LCLIntf, LCLType;
  2.  
  3. type
  4.   PData = ^TData;
  5.   TData = record
  6.      Text: String[255];
  7.     //add other variables if needed
  8.     Button: TButton;
  9.   end;
  10.  
  11. procedure TForm1.FormCreate(Sender: TObject);
  12. var
  13.   Data: PData;
  14.   ListItem: TListItem;
  15. begin
  16.   //add item with button
  17.   GetMem(Data, SizeOf(TData));
  18.   Data^.Button := TButton.Create(nil);
  19.   Data^.Button.Caption := "Click me!!!";
  20.   ListItem := ListView1.Items.Add;
  21.   ListItem.Caption := "line1";
  22.   ListItem.Data := Data;
  23.  
  24.   //add item without button
  25.   Data := nil;
  26.   ListItem := ListView1.Items.Add;
  27.   ListItem.Caption := "line2";
  28.   ListItem.Subitems.Add("no button");
  29.   ListItem.Data := Data;
  30. end;
  31.  
  32. procedure TForm1.FormDestroy(Sender: TObject);
  33. var
  34.   Data: PData;
  35.   I: Integer;
  36. begin
  37.   for I := ListView1.Items.Count - 1 downto 0 do
  38.   begin
  39.     Data := ListView1.Items.Item[I].Data;
  40.     if Data <> nil then
  41.     begin
  42.       Data^.Button.Free;
  43.       Freemem(Data);
  44.     end;
  45.     ListView1.Items.Delete(I);
  46.   end;
  47. end;
  48.  
  49. procedure TForm1.ListView1CustomDrawSubItem(Sender: TCustomListView;
  50.   Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  51.   var DefaultDraw: Boolean);
  52. var
  53.   R: TRect;
  54.   Data: PData;
  55. begin
  56.   Data := Item.Data;
  57.   if Data = nil then
  58.   begin
  59.     DefaultDraw := True;
  60.     Exit;
  61.   end;
  62.  
  63.   DefaultDraw := False;
  64.   R := Item.DisplayRectSubItem(1, drBounds);
  65.   InflateRect(R, -1, -1);
  66.   Data^.Button.Parent := ListView1;
  67.   Data^.Button.SetBounds(R.Left, R.Top, R.Right - R.Left, R.Bottom - R.Top);
  68.   Data^.Button.OnClick := @DoOnButtonClick;
  69. end;
  70.  
  71. procedure TForm1.DoOnButtonClick(Sender: TObject);
  72. begin
  73.   ShowMessage("Boom!!!");
  74. end;                                  

pcurtis

  • Hero Member
  • *****
  • Posts: 935
[SOLVED] Re: Adding a button to certain items of TListView
« Reply #3 on: February 12, 2022, 01:11:50 pm »
@getmem Just what I'm looking for. How can I add an extra column also with a button?
« Last Edit: February 12, 2022, 09:33:24 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018