2

私は Lazarus 0.9.30 を実行しています。

フォームに標準の TStringGrid があり、実行時に TGridColumns オブジェクトを動的に追加する関数があります。各列のすべての属性 (実行時にファイルから読み取る) を含むオブジェクトのコレクションがあり、各オブジェクトを対応する列ヘッダーに関連付けたいと考えています。

以下のコードを試してみましたが、実行時に列ヘッダー オブジェクトの背後にあるオブジェクトにアクセスしようとすると、'nil オブジェクトが返されます。これが発生する理由は、(列のタイトルを保持する) グリッド セルが空白であり、空のグリッド セルにオブジェクトを関連付けることができないためだと思われます。

type
  TTmColumnTitles = class(TTmCollection)
  public
    constructor Create;
    destructor  Destroy; override;

    function  stGetHint(anIndex : integer) : string;
  end;

type
  TTmColumnTitle = class(TTmObject)
  private
    FCaption         : string;
    FCellWidth       : integer;
    FCellHeight      : integer;
    FFontOrientation : integer;
    FLayout          : TTextLayout;
    FAlignment       : TAlignment;
    FHint            : string;

    procedure vInitialise;

  public
    property stCaption        : string      read FCaption         write FCaption;
    property iCellWidth       : integer     read FCellWidth       write FCellWidth;
    property iCellHeight      : integer     read FCellHeight      write FCellHeight;
    property iFontOrientation : integer     read FFontOrientation write FFontOrientation;
    property Layout           : TTextLayout read FLayout          write FLayout;
    property Alignment        : TAlignment  read FAlignment       write FAlignment;
    property stHint           : string      read FHint            write FHint;

    constructor Create;
    destructor  Destroy; override;
  end;

procedure TTmMainForm.vLoadGridColumnTitles
  (
  aGrid       : TStringGrid;
  aCollection : TTmColumnTitles
  );
var
  GridColumn   : TGridColumn;
  aColumnTitle : TTmColumnTitle; //Just a pointer!
  anIndex1     : integer;
  anIndex2     : integer;
begin
  for anIndex1 := 0 to aCollection.Count - 1 do
    begin
      aColumnTitle := TTmColumnTitle(aCollection.Items[anIndex1]);

      GridColumn := aGrid.Columns.Add;
      GridColumn.Width := aColumnTitle.iCellWidth;
      GridColumn.Title.Font.Orientation := aColumnTitle.iFontOrientation;
      GridColumn.Title.Layout           := aColumnTitle.Layout;
      GridColumn.Title.Alignment        := aColumnTitle.Alignment;
      GridColumn.Title.Caption          := aColumnTitle.stCaption;

      aGrid.RowHeights[0] := aColumnTitle.iCellHeight;
      aGrid.Objects[anIndex1, 0] := aColumnTitle;
    end; {for}
end;
4

1 に答える 1

2

Objectsプロパティにオブジェクトを割り当てるだけでは十分ではありません。イベント ハンドラーでそのオブジェクトからタイトル キャプションを自分で描画するか、プロパティもOnDrawCell割り当てる必要があります。Cells

空のグリッド セルにオブジェクトを関連付けることはできません。

はい、できます。文字列と 1 つのセルのオブジェクトは、互いに独立して「機能」します。

したがって、次のようになります。

  for anIndex2 := 0 to aGrid.ColCount - 1 do 
  begin
    aColumnTitle := aCollection.Items[anIndex2];   // Is aCollection.Count in sync
                                                   // with aGrid.ColCount??
    aGrid.Cells[anIndex2, 0] := aColumnTitle.Caption;    
    aGrid.Objects[anIndex2, 0] := aColumnTitle;
  end;
于 2012-02-27T10:15:15.233 に答える