?
Solved

Colored StringGrid

Posted on 2003-03-15
4
Medium Priority
?
618 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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