学用 TStringGrid [6] - Options

本例运行效果图:

26153942_gIYS.gif

一般修改 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

 
 
 
 
 

 

 

  

转载于:https://my.oschina.net/hermer/blog/320869

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值