In Delphi, you can create a TListView and add TListItem items at runtime using the following steps:
- Create a Form:
Create a new VCL Forms Application in Delphi and drop aTListViewcomponent on the form. - Add Code to Create
TListViewandTListItemDynamically:
You can use the following code in the form’s unit to create aTListViewand addTListItemitems dynamically:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950unit MainForm;interfaceusesWinapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls;typeTForm1 = class(TForm)Button1: TButton;procedure Button1Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);varMyListView: TListView;MyListItem: TListItem;I: Integer;begin// Create a TListView dynamicallyMyListView := TListView.Create(Self);MyListView.Parent := Self;MyListView.Align := alClient;MyListView.ViewStyle := vsReport;MyListView.Columns.Add.Caption := ‘Column 1’;// Create and add TListItem items dynamicallyfor I := 1 to 10 dobeginMyListItem := MyListView.Items.Add;MyListItem.Caption := ‘Item ‘ + IntToStr(I);MyListItem.SubItems.Add(‘Subitem 1’);MyListItem.SubItems.Add(‘Subitem 2’);// Add more subitems as neededend;end;end.
In this example, clicking theButton1on the form dynamically creates aTListViewwith one column and adds tenTListItemitems with some subitems.Remember to adjust the code based on your specific requirements and customize it as needed for your application. This is a basic example, and you can enhance it with additional features, formatting, and event handling based on your application’s needs.
Leave a Reply