Re: TZF - TzGrid Colors

[email protected] Tue, 4 Nov 2003 21:15:37 EST
Newsgroups gmane.comp.lang.delphi.topaz
Message-ID <[email protected]>
>Hi Bob,

>I think the 'Table1size' variable refers to a 'Field' in the grid.  So 
>you need to substitute a valid field reference from your database.

>Here's some alternate code for changing colors:
>-----------------------------------------------------
>Change Color of Grid Cell

<SNIP>

>Maybe one of these, or a combination, will get you what you want.

>Marie
______________________________________________________
______________________________________________________




Thanks Marie - 
That hit the spot.  I modified one of your examples slightly
and I moved very close to my goal.  The following code will make the
"current row" a different color, and the selected field an even different
color.  There is a problem with scrolling, which is explained after the code.


// &&&&&&&&&&&&&&&&&&&&
unit U_Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, atzgrids, DB, tzprimds, ucommon, utzcds, utzfds;

type
  TForm1 = class(TForm)
    TzDbf1: TTzDbf;
    DataSource1: TDataSource;
    TzGrid1: TTzGrid;
    procedure FormActivate(Sender: TObject);
    procedure TzGrid1ChangeColorsEvent(Sender: TObject; curRow: Integer;
      var IndicatorColor, CellColor, CellTextColor, HiliteColor,
      HiliteTextColor, DeletedColor, DeletedTextColor: TColor);
    procedure TzDbf1AfterScroll(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
    globCurrentMasterRecordNo:LongInt;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormActivate(Sender: TObject);
begin
  tzDBF1.DbfFileName:='.\Games.dbf';
  tzDBF1.Active:=True;
end;

procedure TForm1.TzGrid1ChangeColorsEvent(Sender: TObject; curRow: Integer;
  var IndicatorColor, CellColor, CellTextColor, HiliteColor,
  HiliteTextColor, DeletedColor, DeletedTextColor: TColor);
begin
    if tzDBF1.recNo=globCurrentMasterRecordNo then
    begin
         CellColor:=$0033FFFF;
         DeletedColor:=clRed;
         HiliteColor:=$00ffff01;
         CellTextColor:=clBLACK;
         HiliteTextColor:=clBLACK;
    end
    else
    begin
          CellColor:=$00B3F8FF;
          HiliteColor:=$00B3F8FF;
          CellTextColor:=clBlack;
          HiliteTextColor:=clBlack;
    end;
end;

procedure TForm1.TzDbf1AfterScroll(DataSet: TDataSet);
begin
  globCurrentMasterRecordNo:=tzDBF1.RecNo;
end;

end.
// &&&&&&&&&&  end of code &&&&&&&&



There is still one problem.  Whenever you use the keyboard
up and down arrow to navigate thru the dataset, everything 
works fine.  But if you use
the up and down arrows on the grid scroll bar, it does not
work as desired ....Also, if you put a SKIP button on
the form it does not work as desired ...  its close, but not quite.  
Must have something to do with the way
the "events occur".  

In case you are further interested, you
can download the project files, a DBF file and an EXE file
in the form of a zip file at:

  http://www.mindspring.com/~au_fan/rowColor/


Thanks so much for the assistance,
Bob Watson

______________________________________________________
______________________________________________________

Hi Bob,

I think the 'Table1size' variable refers to a 'Field' in the grid.  So 
you need to substitute a valid field reference from your database.

Here's some alternate code for changing colors:
-----------------------------------------------------
Change Color of Grid Cell

procedure TCountForm1.TzGrid1DrawDataCell(Sender: TObject;
   const Rect: TRect; Field: TField; State: TGridDrawState);
var  s : array[0..255] of char;
      FieldAsString : string;
begin
    with (sender as TTzGrid).canvas do begin
       FieldAsString := Field.AsString;
       if GetDBaseType(Field) in ['N'] then   {a number}
  Brush.Color := clAqua;
       FillRect(Rect);
       ExtTextOut(Handle, Rect.Left + 2,Rect.Top + 2,
          ETO_OPAQUE or ETO_CLIPPED, @Rect,
          StrPCopy(S, FieldAsString), Length(FieldAsString), nil);
    end;
end;
--------------------------------------------
Display a DBGrid column in a different color

# First, put the DefaultDrawing property of the DBGrid to False
# Later, put this code into OnDrawDataCell event of the DBGrid:

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
              Field: TField; State: TGridDrawState);
begin
    { NOMBRE this the field name to paint in a diferent color }
    if Field.FieldName = 'NOMBRE' then
       (Sender as TDBGrid).Canvas.Font.Color := clRed;
       (Sender as TDBGrid).Canvas.TextRect(Rect, Rect.Left + 2,
                  Rect.Top + 2, Field.AsString);
end;
------------------------------------------------------------
Alternating colors in a grid

procedure TForm1.TzGrid1ChangeColorsEvent(Sender: TObject; curRow: Integer;
   var IndicatorColor, CellColor, CellTextColor, HiliteColor,
   HiliteTextColor, DeletedColor, DeletedTextColor: TColor);
begin

    With (sender as TTzGrid) do begin
      if (CurRow MOD 2)=0 then begin
          CellColor:=$00B3F8FF;
          HiliteColor:=$00B3F8FF;
          CellTextColor:=clBlack;
          HiliteTextColor:=clBlack;
          end
      else begin
        CellColor:=$00E1E1C6;
        HiliteColor:=$00E1E1C6;
        CellTextColor:=clBlack;
         HiliteTextColor:=clBlack;
        end;
    end;
    if TzDbf1.Deleted then begin
      if (curRow MOD 2)=0 then begin
         DeletedColor:=$00B3F8FF;
         HiliteColor:=$00B3F8FF;
         CellTextColor:=clBlack;
          HiliteTextColor:=clBlack;
         end
      else begin
         DeletedColor:=$00E1E1C6;
         HiliteColor:=$00E1E1C6;
         CellTextColor:=clBlack;
          HiliteTextColor:=clBlack;
         end;
    end;
end;
----------------------------------------------------

Maybe one of these, or a combination, will get you what you want.

Marie

--^----------------------------------------------------------------
This email was sent to: [email protected]

EASY UNSUBSCRIBE click here: http://topica.com/u/?clvXPN.a5kvff.Z2NsZHQt
Or send an email to: [email protected]

TOPICA - Start your own email discussion group. FREE!
http://www.topica.com/partner/tag02/create/index2.html
--^----------------------------------------------------------------