deepla deepla

Development of useful applet application.

トップ > 開発メモ - Lazarus > コンポーネント - StringGrid

コンポーネント - StringGrid

グリッドを表示するコンポーネントです。データベースを使用する場合は、DGBridを使用します。

文字のアライメント

セル毎に文字のアライメントを指定できます。PrepareCanvasイベントリーを作成し、その関数を使用します。 下記のサンプルは、1列目を左寄せ、2列目を中央寄せ、3列目を右寄せにします。
procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer; aState: TGridDrawState);
var
  ts: TTextStyle;
begin
  ts := StringGrid1.Canvas.TextStyle;
  if aCol = 1 then ts.Alignment := taLeftJustify;
  if aCol = 2 then ts.Alignment := taCenter;
  if aCol = 3 then ts.Alignment := taRightJustify;
  StringGrid1.Canvas.TextStyle := ts;
end;

文字の色

セル毎に文字の色を指定できます。PrepareCanvasイベントリーを作成し、その関数を使用します。 下記のサンプルは、1列目を赤色、2列目を青色にします。
procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer; aState: TGridDrawState);
begin
  if aCol = 1 then StringGrid1.Canvas.Font.Color := clRed;
  if aCol = 2 then StringGrid1.Canvas.Font.Color := clBlue;
end;

ボタン

カーソルがあるセルにボタンを表示します。この機能は、StringGridが入力モードでなければなりません。 下記のサンプルは、3列目にカーソルが移動するとボタンが表示され、そのボタンをクリックするとメッセージが表示されます。
procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.Options := StringGrid1.Options
                         + [goEditing]
                         + [goAlwaysShowEditor];
end;

procedure TForm1.StringGrid1SelectEditor(Sender: TObject; aCol, aRow: Integer; var Editor: TWinControl);
begin
  if aCol = 3 then Editor := StringGrid1.EditorByStyle(cbsEllipsis);
end;

procedure TForm1.StringGrid1EditButtonClick(Sender: TObject);
begin
  if StringGrid1.Col = 3 then ShowMessage('Column 3 button clicked');
end;

PickListボタン

カーソルがあるセルにPickListボタンを表示します。この機能は、StringGridが入力モードでなければなりません。 下記のサンプルは、4列目にカーソルが移動するとDropDownボタンが表示され、そのボタンをクリックすると選択することができます。 さらに、偶数行は入力可能なDropDownボタンで、奇数行は入力不可能なDropDownボタンで表示されます。
procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.Options := StringGrid1.Options
                         + [goEditing]
                         + [goAlwaysShowEditor];
end;

procedure TForm1.StringGrid1SelectEditor(Sender: TObject; aCol, aRow: Integer; var Editor: TWinControl);
begin
  if aCol = 4 then begin
    Editor := StringGrid1.EditorByStyle(cbsPickList);
    if (Editor is TCustomComboBox) then // uses節にStdCtrlsを追加
      with Editor as TCustomComboBox do begin
        if (aRow mod 2 = 0) then
          Style := csDropDown
        else
          Style := csDropDownList;
        case aRow of
          1:
            Items.CommaText := 'ONE,TWO,THREE,FOUR';
          2:
            Items.CommaText := 'A,B,C,D,E';
          3:
            Items.CommaText := 'Lazarus,Delphi,Pascal';
          4:
            Items.CommaText := 'RED,GREEN,BLUE,YELLOW';
        end;
      end;
      // 他オブジェクトからフォーカスが戻ると内容が消えるので以下の処理を行う
      with Sender as TStringGrid do
        Editor.Caption := Cells[aCol, aRow];
  end;
end;

StringGridの変更を検出

下記がサンプルです。
...
  private
    CellInitValue: String;
...

procedure TForm1.StringGrid1Selection(Sender: TObject; aCol, aRow: Integer);
begin
  with Sender as TStringGrid do
    CellInitValue := Cells[aCol, aRow];
end;

procedure TForm1.StringGrid1SetEditText(Sender: TObject; aCol, aRow: Integer; const Value: string);
begin
  if Value <> CellInitValue then begin
    // 処理内容を記述
    ...
    CellInitValue := Value;
  end;
end;

広告リンク

準備

日本語処理

コンポーネント

関連ホームページ

ページのトップへ戻る
inserted by FC2 system