本例效果图:
代码文件:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls; type TForm1 = class(TForm) ListView1: TListView; Button1: TButton; Button2: TButton; CheckBox1: TCheckBox; CheckBox2: TCheckBox; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure CheckBox1Click(Sender: TObject); procedure CheckBox2Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var i: Integer; column: TListColumn; begin for i := 0 to 5 do begin column := ListView1.Columns.Add; column.Caption := Format('Col %d', [i]); end; ListView1.Align := alTop; ListView1.ViewStyle := vsReport; ListView1.GridLines := True; {非默认} ListView1.ShowColumnHeaders := True; {默认} CheckBox1.Caption := 'GridLines'; CheckBox2.Caption := 'ShowColumnHeaders'; CheckBox1.Checked := True; CheckBox2.Checked := True; Button1.Caption := '添加'; Button2.Caption := '清空'; end; procedure TForm1.Button1Click(Sender: TObject); var item: TListItem; i: Integer; begin item := ListView1.Items.Add; item.Caption := 'Item ' + IntToStr(item.Index); for i := 0 to ListView1.Columns.Count - 1 do item.SubItems.Add(Format('%d%d', [item.Index, i])); end; procedure TForm1.Button2Click(Sender: TObject); begin ListView1.Items.Clear; end; procedure TForm1.CheckBox1Click(Sender: TObject); begin ListView1.GridLines := CheckBox1.Checked; end; procedure TForm1.CheckBox2Click(Sender: TObject); begin ListView1.ShowColumnHeaders := CheckBox2.Checked; end; end.
窗体文件:
object Form1: TForm1 Left = 0 Top = 0 Caption = 'Form1' ClientHeight = 198 ClientWidth = 322 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object ListView1: TListView Left = 8 Top = 8 Width = 185 Height = 121 Columns = <> TabOrder = 0 end object Button1: TButton Left = 232 Top = 134 Width = 75 Height = 25 Caption = 'Button1' TabOrder = 1 OnClick = Button1Click end object Button2: TButton Left = 232 Top = 165 Width = 75 Height = 25 Caption = 'Button2' TabOrder = 2 OnClick = Button2Click end object CheckBox1: TCheckBox Left = 16 Top = 145 Width = 97 Height = 17 Caption = 'CheckBox1' TabOrder = 3 OnClick = CheckBox1Click end object CheckBox2: TCheckBox Left = 16 Top = 169 Width = 177 Height = 17 Caption = 'CheckBox2' TabOrder = 4 OnClick = CheckBox2Click end end