Solved

StringGrid Cell Color Change

Posted on 2004-04-03
5
2,003 Views
Last Modified: 2007-11-27
Hi,

I cannot work out how to change the color of a cell in a stringgrid.

I want the program to place values from a string into a cell and
if the value is say 10 then the cell will change to clRed.

I have read numerious postings on the internet that use
StringGrid1DrawCell

But I cannot get them to work.
If I use this  StringGrid1.color I know this changes the whole grid.

Can anyone HELP !!

Thanks
Bruce
0
Comment
Question by:delphimate
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:PerryDK
ID: 10750180
I'm completly new to this and this is my first post.  I assume your using Delphi just from your user name.  I wrote your solution in C++ Builder but you should be able to very easily convert it to Delphi.  For the most part just change the -> to .  If you need more help converting it Delphi I'll convert it for you.

Anyway to create this example drop a string grid on your form.  Create a method to initilize the string grid.  And create a OnDrawCell event code handler for your String grid.  This example simply puts 10 random numbers in column 2 and then sets the background color to red if the number is <= 5.

Again let me know if you need help converting to delphi.  Maybe sombody can tell me how to tell from a post what language they are using java, c++, c#, Delphi, etc.

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  InitializeStringGrid();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::InitializeStringGrid()
{
  StringGrid1->FixedCols = 2;
  StringGrid1->ColCount = 2;
  StringGrid1->Cells[0][0] = "ID";
  StringGrid1->Cells[1][0] = "Random Num";

  //fill the string grid with 10 random numbers
  //the random numbers will be stored in the second column
  StringGrid1->RowCount = 11;
  for(int i = 0; i < 10; i ++)
  {
    StringGrid1->Cells[0][i+1] = i;
    StringGrid1->Cells[1][i+1] = rand() % 10;
  }
}

void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
      int ARow, TRect &Rect, TGridDrawState State)
{
  //were only intersted in making the cell red if the number is less than
  //or equal to 5.  Since the id's are stored in col one they are of no interest
  if(ACol != 1)
    return;

  //not interested in first row
  if(ARow == 0)
    return;

  int value = StrToInt(StringGrid1->Cells[ACol][ARow]);
  if(value <= 5)
  {
    //make the cell red
    StringGrid1->Canvas->Brush->Color = clRed;
    StringGrid1->Canvas->FillRect(Rect);

    //since we just drew over the entire cell we need to redraw our string on top of the red
    StringGrid1->Canvas->Pen->Color = clBlack;
    StringGrid1->Canvas->TextOut(Rect.Left + 2, Rect.Top + 2, StringGrid1->Cells[ACol][ARow]);
  }

}
0
 

Author Comment

by:delphimate
ID: 10750262
Perry,


I am using Delphi 7 and would appreciate if you or somebody could convert your code.

Another questions I have seen this procedure before and don't understand the
line.
StringGrid1->Canvas->TextOut(Rect.Left + 2, Rect.Top + 2, StringGrid1->Cells[ACol][ARow]);

Rec.Left+2  ?


Thanks
Bruce
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10750444
Description of TextOut taken from help
//------------------------------------------------------
Writes a string on the canvas, starting at the point (X,Y), and then updates the PenPos to the end of the string.

procedure TextOut(X, Y: Integer; const Text: string);

Description

Use TextOut to write a string onto the canvas. The string will be written using the current value of Font. Use the TextExtent method to determine the space occupied by the text in the image. To write only the text that fits within a clipping rectangle, use TextRect instead
//------------------------------------------------------

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

Rect is a TRect look up TRect to see what variables are accessable from it in the help file.
Basically it gives you the the top, left, width, and heght of the current cell that you are drawing in coordiantes specific to your TStringGrid

The reason we have to use TextOut is that when we are drawing a cell we first fill it with a red color...this completly hides the default drawing that has occurred so we need to redraw our number "string" back on top of the red stuff in the cell.  The default uses the top and left coordinates and adds 2 to each to give a little bit of an offset so your string isn't directly at the upper left of each cell.  Its just done for appearnce reasons you can change whatever you like including changing the font to yellow for numbers below 3 if you want.

been an awful long time since I programmed in delphi and couldent figure out how to create a function/procedure that takes no arguements so I declared the init as this.

 procedure InitStringGrid(StringGrid1: TStringGrid);


At any rate code listing in Delphi is submittted  below if you have more questions just ask.


//Code Listing

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure InitStringGrid(StringGrid1: TStringGrid);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  InitStringGrid(StringGrid1);
end;

procedure TForm1.InitStringGrid(StringGrid1: TStringGrid);
var
  i: Integer;
begin
  StringGrid1.FixedCols := 2;
  StringGrid1.ColCount := 2;
  StringGrid1.Cells[0, 0] := 'ID';
  StringGrid1.Cells[1, 0] := 'Random Num';

  //fill the string grid with 10 random numbers
  //the random numbers will be stored in the second column
  StringGrid1.RowCount := 11;
  for i := 0 to 9 do
  begin
    StringGrid1.Cells[0, i+1] := IntToStr(i);
    StringGrid1.Cells[1, i+1] := IntToStr(Random(10));
  end;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
   value : Integer;
begin
  //were only intersted in making the cell red if the number is less than
  //or equal to 5.  Since the id's are stored in col one they are of no interest
  if ACol <> 1 then
    exit;

  //not interested in first row
  if ARow = 0 then
    exit;

  value := StrToInt(StringGrid1.Cells[ACol, ARow]);
  if value <= 5 then
  begin
    StringGrid1.Canvas.Brush.Color := clRed;
    StringGrid1.Canvas.FillRect(Rect);
    StringGrid1.Canvas.Pen.Color := clBlack;
    StringGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, StringGrid1.Cells[ACol, ARow]);
  end;
end;

end.
0
 
LVL 4

Accepted Solution

by:
PerryDK earned 250 total points
ID: 10755147
Did this answer your question if so please close the question...if not ask more info so that I may be of assistance.
0
 

Author Comment

by:delphimate
ID: 10762674
Perry,

Thanks for all your help !!

I have sorted it out.


Bruce.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now