Jump to content
karl Jonson

TListView with check boxes

Recommended Posts

Hi,
Does anyone have a working logic for Tlistview with checkboxes where a user can multi select options similar to these:
None
Not Known
OptionOne
OptionTwo

 

Thanks

Share this post


Link to post
2 hours ago, karl Jonson said:

Hi,
Does anyone have a working logic for Tlistview with checkboxes where a user can multi select options similar to these:
None
Not Known
OptionOne
OptionTwo

 

Thanks

 

 

unit TestbedU1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Classes,
  Vcl.ComCtrls;

type
  TForm1 = class(TForm)
    ViewButton: TButton;
    Edit1: TEdit;
    ListView1: TListView;
    Memo1: TMemo;
    CheckButton: TButton;
    UncheckButton: TButton;
    procedure CheckButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure UncheckButtonClick(Sender: TObject);
    procedure ViewButtonClick(Sender: TObject);
  strict private
    procedure SetCheckstate(aListview: TListView; Value: Boolean);
  private
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.dfm}
procedure TForm1.CheckButtonClick(Sender: TObject);
begin
  SetCheckstate(listview1, true);
end;

procedure TForm1.FormCreate(Sender: TObject);
const
  ColCaptions: array [0..3] of string = (
    'Zero','One','Two','Three');
var
  I: Integer;
  LCol: TListColumn;
  LItem: TListItem;
  N: Integer;
begin
  memo1.Clear;
  listview1.ViewStyle := vsReport;
  listview1.Checkboxes := true;
  listview1.MultiSelect := true;
  for I := 0 to 3 do begin
    LCol:= listview1.Columns.Add;
    LCol.Caption := ColCaptions[I];
    LCol.AutoSize := true;
  end;
  for I := 1 to 5 do begin
    LItem:= listview1.Items.Add;
    LItem.Caption := Format('Item %d',[I]);
    for N := 1 to 3 do
      LItem.SubItems.Add(Format('Item %d%d',[I, N]));
  end;
end;

procedure TForm1.ViewButtonClick(Sender: TObject);
const
  State: array [boolean] of string = ('not','');
var
  I: Integer;
  LItem: TListItem;
begin
  for I := 0 to listview1.items.count-1 do begin
    LItem:= ListView1.Items[I];
    memo1.Lines.Add(
      Format( 'Item %d is %s checked.', [Succ(I), State[LItem.Checked]])
      );
  end;
end;

procedure TForm1.SetCheckstate(aListview: TListView; Value: Boolean);
var
  I: Integer;
  LItem: TListItem;
begin
  for I := 0 to listview1.items.count-1 do begin
    LItem:= ListView1.Items[I];
    if LItem.Selected then
       LItem.Checked := Value;
  end;
end;

procedure TForm1.UncheckButtonClick(Sender: TObject);
begin
  SetCheckstate(listview1, false);
end;

end.

 

TestbedU1.dfm

TestbedU1.pas

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×