Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Colored StringGrid

Posted on 2003-03-15
4
Medium Priority
?
634 Views
Last Modified: 2010-04-03
Hi
I need for my application a stringgrid with alternative colors in each row, but my grid needs to be in a goAlwaysShowEditor := True codition. But when a cell is being edited turns white with black text. I got something like this:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin

if (gdFocused in State) then begin
  if (Arow mod 2 = 1) then begin
    StringGrid1.Canvas.Brush.Color := clYellow;
    StringGrid1.Canvas.Font.Color := clMaroon;
  end
  else begin
    StringGrid1.Canvas.Brush.Color := clWhite;
    StringGrid1.Canvas.Font.Color := clBlack;
  end;
end
else
  if (Arow mod 2 = 1) then begin
    StringGrid1.Canvas.Brush.Color := clYellow;
    StringGrid1.Canvas.Font.Color := clMaroon;
  end
  else begin
    StringGrid1.Canvas.Brush.Color := clWhite;
    StringGrid1.Canvas.Font.Color := clBlack;
  end;

If (ARow>0) then
 begin
   StringGrid1.canvas.fillRect(Rect);
   StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
 end;
end;

I need that the cell that is being edited do not change color. Please help, I am looking for this solution on-line for 2 days now and there is only the same answer again and again State = [gdFocused], but that does not helps.
0
Comment
Question by:sguerra
  • 3
4 Comments
 
LVL 9

Accepted Solution

by:
mocarts earned 300 total points
ID: 8146135
type
  TMyStringGrid = class(TStringGrid)
  end;
  TMyInplaceEdit = class(TInplaceEdit)
  end;
function GetBrushColor(const Row: integer): TColor;
begin
 if (Row mod 2 = 1) then
   Result := clYellow
 else
   Result := clWhite;
end;
function GetPenColor(const Row: integer): TColor;
begin
 if (Row mod 2 = 1) then
   Result := clMaroon
 else
   Result := clBlack;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  StringGrid1.Canvas.Brush.Color := GetBrushColor(ARow);
  StringGrid1.Canvas.Font.Color := GetPenColor(ARow);

  if (ARow>0) then
  begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;

end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  CanSelect := True;
  if assigned(TMyStringGrid(StringGrid1).InplaceEditor) then
    with TMyInplaceEdit(TMyStringGrid(StringGrid1).InplaceEditor) do begin
      Color := GetBrushColor(ARow);
      Font.Color := GetPenColor(ARow);
    end;
end;

there is a problem to set color when editor is created for first time (as SelectCell ocurs befor editor is created), but I haven't time at the moment to fix this.
maybe later :)

wbr, mo.
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8146784
solved :) instead of OnSelectCell use OnGetEditText event of TStringGrid.

procedure TForm1.StringGrid1GetEditText(Sender: TObject; ACol,
  ARow: Integer; var Value: String);
begin
  if assigned(TMyStringGrid(StringGrid1).InplaceEditor) then
    with TMyInplaceEdit(TMyStringGrid(StringGrid1).InplaceEditor) do begin
      Color := GetBrushColor(ARow);
      Font.Color := GetPenColor(ARow);
    end;
end;

wbr, mo.
0
 
LVL 1

Author Comment

by:sguerra
ID: 8151814
Thanks mocarts, it worked perfectly
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8604349
don't forget to accept answer ;)
wbr, mo.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi databaseā€¦
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calculā€¦
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question