Hello everybody! “Quick Tips” On this I will show how we can read and write text in a “ListView” in a practical and fast.
Let's create a Form (save as uFrmPrincipal) and create it:
* 3 “TLabeledEdit” (EdtCode, EdtDescription, EdtValue);
* 3 “TBitBtn” (BtnSaveRegister, BtnDeleteRegister, BtnLoadRegister);
* 1 “TListView” (ListViewExample).
We will now configure the properties of “ListViewExample”.
Add 3 columns (Code, Description and Value):
Property - ViewStyle = vsReport;
Property - SortType = stText;
We can see the complete example:

Unit’s Implementation:
unit uFrmPrincipal;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls, Buttons;
type
TFrmPrincipal = class(TForm)
EdtCode: TLabeledEdit;
EdtDescription: TLabeledEdit;
EdtValue: TLabeledEdit;
Panel1: TPanel;
ListViewExemplo: TListView;
BtnSaveRegister: TBitBtn;
BtnDeleteRegister: TBitBtn;
BtnLoadRegister: TBitBtn;
procedure BtnSaveRegisterClick(Sender: TObject);
procedure BtnDeleteRegisterClick(Sender: TObject);
procedure BtnLoadRegisterClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FrmPrincipal: TFrmPrincipal;
implementation
{$R *.dfm}
{Save Register in the ListView}
procedure TFrmPrincipal.BtnSaveRegisterClick(Sender: TObject);
Var
Item: TListItem;
begin
Item := ListViewExample.Items.Add;
Item.Caption := EdtCode.Text;
Item.SubItems.Add(EdtDescription.Text);
Item.SubItems.Add(EdtValuer.Text);
EdtCode.Clear;
EdtDescription.Clear;
EdtValue.Clear;
end;
{Delete Register of the ListView}
procedure TFrmPrincipal.BtnDeleteRegisterClick(Sender: TObject);
begin
if ListViewExample.ItemIndex >= 0 then
ListViewExample.DeleteSelected
else
ShowMessage('Select an item to delete');
end;
{Load Register of the ListView}
procedure TFrmPrincipal.BtnLoadRegisterClick(Sender: TObject);
begin
if ListViewExample.ItemIndex >= 0 then
begin
EdtCode.Text := ListViewExample.ItemFocused.Caption;
EdtDescription.Text := ListViewExample.ItemFocused.SubItems[0];
EdtValue.Text := ListViewExample.ItemFocused.SubItems[1];
end
else
ShowMessage('Could not select any item to load');
end;
end.
Until next Quick Tips.
Wesley Y
wyamazack@rwsolution.com.br

See the prices for this post in Mr.Bool Credits System below: