随笔 - 2146  文章 - 19 评论 - 11846 阅读 - 1267万


本例效果图:



代码文件:
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

posted on   万一  阅读(9628)  评论(6编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧


点击右上角即可分享
微信分享提示