stringgridでセルを比較して一致しなかったら色を付けるには?

解決


超初心者です  2010-09-28 23:29:34  No: 39244

-----------------------------------------
1|    A   |   B    |    C   |    D   |
  -----------------------------------------
2|    A   |   B    |    C   |    D   |
  -----------------------------------------
行のセルAとセルBの中身を比較して、一致しなかったらその行を「赤く」したいです。
一致した場合は何もしません。

色をつけることはできたのですが、どうしても全体に色がついてしまいます。

わかりづらい説明で申し訳ありません。
もしよろしければ教えてください。お願い致します。


igy  2010-09-29 06:37:06  No: 39245

>色をつけることはできたのですが、どうしても全体に色がついてしまいます。

どのようなコードを書いているのですか?


ぽむぽむ  2010-09-29 18:54:29  No: 39246

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if gdFixed in State then Exit;
  // 1 が A列 のセル
  // 2 が B列 のセル
  if StringGrid1.Cells[1, ARow] <> StringGrid1.Cells[2, ARow] then
  begin
    StringGrid1.Canvas.Brush.Color := clRed;
    StringGrid1.Canvas.FillRect(Rect);
    StringGrid1.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2,
      StringGrid1.Cells[ACol, ARow]);
  end;
end;


  2010-09-29 20:43:51  No: 39247

たとえばこうするとか?

  // 1 が A列 のセル
  // 2 が B列 のセル
  if (ACol=1) or (ACol=2) then   
    if StringGrid1.Cells[1, ARow] <> StringGrid1.Cells[2, ARow] then
    begin
      StringGrid1.Canvas.Brush.Color := clRed;
      StringGrid1.Canvas.FillRect(Rect);
      StringGrid1.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2,
          StringGrid1.Cells[ACol, ARow]);
    end;


KHE00221  2010-09-30 08:43:05  No: 39248

実行時にセルの編集は行うのかな?


超初心者です  2010-10-03 02:38:20  No: 39249

お返事遅くなってしまい大変申し訳ありません。
皆様たくさんの回答本当にありがとうございます。

試してみたのですが、stringgridのセル全てが赤くなってしまいました。
当方の質問の仕方が悪かったのかもしれません。
     A     B     C     D
  --------------------------
1|セル1|セル2|セル3|セル4|
  --------------------------

if stringgrid1.cells[1,A(セル1の中身)] <> stringgrid1.cells[1,B(セル2の中身)] then
begin
  セル1を赤く色付け;
  セル2を赤く色付け;
  セル3を赤く色付け;
  セル4を赤く色付け;
end;

このような処理をさせたいのですが、どう記述するのがよいのでしょうか?
何度も申し訳ありません。ぜひ教えてください。


TS  2010-10-03 03:47:20  No: 39250

>超初心者です 
こうゆう名前は嫌われます、何かまともな名前を付けられる事を希望します。

>当方の質問の仕方が悪かったのかもしれません。
恐らく皆さん質問の内容は分かっておられると思います。

>試してみたのですが、stringgridのセル全てが赤くなってしまいました。
igy の質問にも答えられた方が解決が早いと思います。

ぽむぽむ 、華さんの例示された方法は分かって試されましたか。
恐らく超初心者ですと名乗っておられるのでここで分からないのでしたら、
この方法はこうして上手く行きませんがと質問された方が良いかと。

KHE00221 さんの質問にも答えられた方が先に進むと思いますが。


TS  2010-10-03 03:49:09  No: 39251

igy さんを呼び捨てました、失礼しました。


久美子  2010-10-03 05:29:48  No: 39252

ぽむぽむ さんの方法で、解決しているような気がしますが、ためしに、FormにButton1sとstringGrid1(ColCount は 5、RowCount は 10に設定)を貼り付けて、次のエベントを設定して試してみてください。

procedure TForm1.Button1Click(Sender: TObject);
var
  R,C: Integer;
begin
  Randomize;
  for R := 1 to 10 do
    for C := 1 to 4 do
      begin
          StringGrid1.Cells[C, R] := intToStr(Random(5));
      end;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if gdFixed in State then Exit;
  // 1 が A列 のセル
  // 2 が B列 のセル
  if StringGrid1.Cells[1, ARow] <> StringGrid1.Cells[2, ARow] then
  begin
    StringGrid1.Canvas.Brush.Color := clRed;
    StringGrid1.Canvas.FillRect(Rect);
    StringGrid1.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, StringGrid1.Cells[ACol, ARow]);
  end;
end;


KHE00221  2010-10-03 17:57:25  No: 39253

ほれ

  TInplaceEdit = class(Grids.TInplaceEdit);

  TComparisonStyle = (csNone,csENTER,csREAL);

  TStringGrid = class(Grids.TStringGrid)
  private
    FSaveRow: Integer;
    FSaveCol: Integer;
    FComparisonColor: TColor;
    FComparison: TComparisonStyle;
    FComparisonCol1: Integer;
    FComparisonCol2: Integer;
    procedure SetComparisonColor(Value: TColor);
    procedure SetComparison(Value: TComparisonStyle);
    procedure SetComparisonCol1(Value: Integer);
    procedure SetComparisonCol2(Value: Integer);
  protected
    procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
      AState: TGridDrawState); override;
    function GetEditText(ACol, ARow: Longint): string; override;
    procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
    procedure KeyPress(var Key: Char); override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property Comparison: TComparisonStyle read FComparison write SetComparison;
    property ComparisonColor: TColor read FComparisonColor write SetComparisonColor;
    property ComparisonCol1: Integer read FComparisonCol1 write SetComparisonCol1;
    property ComparisonCol2: Integer read FComparisonCol2 write SetComparisonCol2;
  end;

//---------------------------------------

procedure TStringGrid.SetComparisonColor(Value: TColor);
begin
    FComparisonColor := Value;
    Invalidate;
end;

procedure TStringGrid.SetComparison(Value: TComparisonStyle);
begin
    FComparison := Value;
    Invalidate;
end;

procedure TStringGrid.SetComparisonCol1(Value: Integer);
begin
    FComparisonCol1 := Value;
    Invalidate;
end;

procedure TStringGrid.SetComparisonCol2(Value: Integer);
begin
    FComparisonCol2 := Value;
    Invalidate;
end;

procedure TStringGrid.KeyPress(var Key: Char);
var
    C: TColor;
begin

    if (Key = #13) and (FComparison = csENTER) then
    begin
      if (FSaveCol <> -1) and (FSaveRow <> -1) then
      begin
        C := Color;
        if Cells[FComparisonCol1, FSaveRow] <> Cells[FComparisonCol2, FSaveRow] then
        begin
          C := FComparisonColor;
        end;
        TInplaceEdit(InplaceEditor).Color := C;
        InvalidateRow(FSaveRow);
      end;
    end;

    inherited KeyPress(Key);
end;

procedure TStringGrid.SetEditText(ACol, ARow: Longint; const Value: string);
var
    C: TColor;
begin
    inherited;

    if (FComparison = csREAL) then
    begin
      C := Color;
      if Cells[FComparisonCol1, ARow] <> Cells[FComparisonCol2, ARow] then
      begin
        C := FComparisonColor;
      end;
      TInplaceEdit(InplaceEditor).Color := C;
      InvalidateRow(FSaveRow);
    end;

end;

function TStringGrid.GetEditText(ACol, ARow: Longint): string;
var
    C: TColor;
begin

    Result := inherited GetEditText(ACol,ARow);

    if (FComparison <> csNONE) then
    begin
      FSaveCol := ACol;
      FSaveRow := ARow;
      C := Color;
      if Cells[FComparisonCol1, ARow] <> Cells[FComparisonCol2, ARow] then
      begin
        C := FComparisonColor;
      end;
      TInplaceEdit(InplaceEditor).Color := C;
    end;

end;

procedure TStringGrid.DrawCell(ACol, ARow: Longint; ARect: TRect;AState: TGridDrawState);
begin
    if (FComparison <> csNONE) then
    begin
      if gdFixed in AState then
      begin
        inherited DrawCell(ACol, ARow, ARect, AState);
      end
      else
      begin
        Canvas.Brush.Color := Color;
        if Cells[FComparisonCol1, ARow] <> Cells[FComparisonCol2, ARow] then
        begin
          Canvas.Brush.Color := FComparisonColor;
        end;
      end;
    end;
    inherited DrawCell(ACol, ARow, ARect, AState);
end;

constructor TStringGrid.Create(AOwner: TComponent);
begin
    inherited;
    FSaveCol := -1;
    FSaveRow := -1;
    FComparison := csREAL;
    FComparisonCol1 := 1;
    FComparisonCol2 := 2;
    FComparisonColor := clRed;
end;


超初心者です  2010-10-05 06:13:00  No: 39254

皆様のおかげで解決することができました。
本当にありがとうございました。


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加