本例运行效果图:
一般修改 TStringGrid 的 Options 直接在设计时选一下 True 或 False 就行了; 代码中可以像下面操作:

一般修改 TStringGrid 的 Options 直接在设计时选一下 True 或 False 就行了; 代码中可以像下面操作:
做完这个例子发现不太初级了, 但代码很简单:StringGrid1.Options := [goFixedVertLine]; StringGrid1.Options := [goFixedVertLine, goVertLine, goColSizing];
附上窗体设计源码:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Grids; type TForm1 = class(TForm) StringGrid1: TStringGrid; GroupBox1: TGroupBox; procedure FormCreate(Sender: TObject); procedure GroupBox1Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} uses TypInfo; {需要这个单元获取 StringGrid1.Options 选项名称} var cb: TCheckBox; {准备动态生成 15 个 TCheckBox} GridOpt: TGridOptions; {StringGrid1.Options 是一个 TGridOptions 类型(集合)} {同时也应知道: TGridOptions 集合是根据 TGridOption 枚举定义的} {窗体建立时, 干了不少事} procedure TForm1.FormCreate(Sender: TObject); var i,j: Integer; gb: TGroupBox; {为简化 GroupBox1 名称用} begin StringGrid1.RowCount := 16; {设 StringGrid1 为 16 行} {给每个单元赋值} with StringGrid1 do for i := 0 to ColCount - 1 do for j := 0 to RowCount - 1 do Cells[i,j] := Format('%.1x%.1x',[i,j]); {下面只是动态生成 15 个 TCheckBox, 并赋予标题与 OnClick 功能} gb := Self.GroupBox1; {用 gb 简化 GroupBox1 的称谓} j := 16; {用来记录 TCheckBox 的纵向位置} for i := 0 to 14 do begin if cb<>nil then Inc(j,cb.Height); cb := TCheckBox.Create(Self); cb.Parent := gb; cb.Left := 6; cb.Top := j; cb.Caption := GetEnumName(TypeInfo(TGridOption),i); cb.OnClick := gb.OnClick; {本来可以没有下面这句, 不然会隐藏部分名称, 可能是中文系统的支持问题} cb.Width := Canvas.TextWidth(GetEnumName(TypeInfo(TGridOption),5))+ 20; end; end; {这个主要是让 TCheckBox 调用的} procedure TForm1.GroupBox1Click(Sender: TObject); var i: Integer; begin GridOpt := []; for i := 0 to GroupBox1.ControlCount - 1 do begin if TCheckBox(GroupBox1.Controls[i]).Checked then GridOpt := GridOpt + [TGridOption(i)]; end; StringGrid1.Options := GridOpt; end; end.
object Form1: TForm1 Left = 201 Top = 31 Caption = 'Form1' ClientHeight = 277 ClientWidth = 468 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False Position = poDesigned OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object StringGrid1: TStringGrid Left = 0 Top = 0 Width = 335 Height = 277 Align = alClient TabOrder = 0 ExplicitWidth = 328 end object GroupBox1: TGroupBox Left = 335 Top = 0 Width = 133 Height = 277 Align = alRight Caption = 'GroupBox1' TabOrder = 1 OnClick = GroupBox1Click ExplicitLeft = 328 end end
分类:
TStringGrid
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· 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吧