Solved

The brush color in  DBGrid

Posted on 2001-06-22
7
559 Views
Last Modified: 2010-04-06
Dear Experts, please help !

   When I entered the edit mode in dbgrid, I wish the background color of the editor in dbgrid changed into another color ( clAqua ).
And this is what I've done, but it doesn't work ?

Thanks from apin

p.s : I intend not to use the OnDrawDataCell or OnDrawColoumnCell procedure.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, Grids, DBGrids, DBTables, StdCtrls;

type
  TForm1 = class(TForm)
    Table1: TTable;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Label1: TLabel;
    procedure FormActivate(Sender: TObject);
    procedure DBGrid1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

 type
   MyDBGrid = class(TDBGrid)
   protected
     function CreateEditor: TInplaceEdit; override;
  end;

 type
   The_InplaceEdit = class(TInplaceEdit)
   public
     property Color ;
 End ;

var
  Form1   : TForm1;

implementation

{$R *.DFM}


function MyDBGrid.CreateEditor: TInplaceEdit;
begin
   Result := inherited CreateEditor;
   The_InplaceEdit(Result).Brush.Color:= clAqua ;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  With MyDBGrid(DBGrid1) Do Begin
     Font.Color := clGreen ;
     Font.Size := 14 ;
  End ;
end;

procedure TForm1.DBGrid1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  B: byte;
begin
  With MyDBGrid(DBGrid1) Do begin
     for B := 0 to ControlCount - 1 do begin
         with Controls[B] as TInPlaceEdit do begin
           Label1.Caption := 'Text = ' + Text;
         end; // With
     end ; // for
  End ;
end;


end.
0
Comment
Question by:pc_melsa
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:TOndrej
ID: 6219275
You have declared a new class MyDBGrid but on your form you put a TDBGrid.
Therefore the grid you see at runtime is an instance of TDBGrid, not MyDBGrid, and typecasting with MyDBGrid(DBGrid1) has no effect; your overriden MyDBGrid.CreateEditor is never called.

What you need to do instead is create an instance of MyDBGrid. Either install MyDBGrid into the component palette and put in on your form just like with TDBGrid, or create it in code, e.g. in the form's OnCreate event:

type
  TForm1 = class(TForm)
  ...
  private
    FMyDBGrid: MyDBGrid;
  end;

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  with MyDBGrid.Create(Self) do
    DataSource := DataSource1;
end;

In your overriden MyDBGrid.CreateEditor it should be enough to do

function MyDBGrid.CreateEditor: TInplaceEdit;
begin
  Result := inherited CreateEditor;
  The_InplaceEdit(Result).Color:= clAqua ;
end;

HTH
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6219282
sorry the declaration of private FMyDBGrid: MyDBGrid in the form is not necessary, I forgot to delete it.

I hope it's not confusing, I'll send another comment with the intended text.
0
 
LVL 8

Accepted Solution

by:
TOndrej earned 100 total points
ID: 6219289
Another try, here's what I intended to post:

You have declared a new class MyDBGrid but on your form you put a TDBGrid.
Therefore the grid you see at runtime is an instance of TDBGrid, not MyDBGrid, and typecasting with
MyDBGrid(DBGrid1) has no effect; your overriden MyDBGrid.CreateEditor is never called.

What you need to do instead is create an instance of MyDBGrid. Either install MyDBGrid into the component
palette and put in on your form just like with TDBGrid, or create it in code, e.g. in the form's OnCreate
event:

procedure TForm1.FormCreate(Sender: TObject);
begin
 with MyDBGrid.Create(Self) do
   DataSource := DataSource1;
end;

In your overriden MyDBGrid.CreateEditor it should be enough to do

function MyDBGrid.CreateEditor: TInplaceEdit;
begin
 Result := inherited CreateEditor;
 The_InplaceEdit(Result).Color:= clAqua ;
end;

HTH
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:pc_melsa
ID: 6222507
Dear Mr. HTH,
Thanks for your explanation, it works ! Below is the component I?ve created. But another problem arise. When I want to change the color ( from clAqua to clRed, for instance ), I must change the unit file of the component and recompile again.
Please help me again.

     
Best Regards,
Apin


unit My_DBGrid;

interface

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

type
  MyDBGrid = class(TDBGrid)

  published
    { Published declarations }
  end;

type
  The_InplaceEdit = class(TInplaceEdit)
  public
    property Color ;
End ;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [MyDBGrid]);
end;

function MyDBGrid.CreateEditor: TInplaceEdit;
begin
  Result := inherited CreateEditor;
  The_InplaceEdit(Result).Brush.Color:= clYellow ;
end;
end.

0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6223476
Simply declare a published property InplaceEditColor:

type
  MyDBGrid = class(TDBGrid)
  private
    FInplaceEditColor: TColor;
  protected
    function CreateEditor: TInplaceEdit;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property InplaceEditColor: TColor read FInplaceEditColor write FInplaceEditColor default clAqua;
  end;

implementation

function MyDBGrid.CreateEditor: TInplaceEdit;
begin
  Result := inherited CreateEditor;
  The_InplaceEdit(Result).Color := FInplaceEditColor;
end;

constructor MyDBGrid.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FInplaceEditColor := clAqua;
end;

Properties declared in the published section are stored with the component into the .dfm stream.
The constructor is really needed only to initialize the property to the default value.
When the component is being saved into the .dfm stream and the property has the value that was declared default then the property is not saved into the .dfm - this saves some space.

HTH = Hope This Helps :-)

TOndrej

P.S. Why grade B?
0
 

Author Comment

by:pc_melsa
ID: 6227007
Dear Mr. TOndrey,
    Thank you very much, this is actually a great help for me !
May God Bless You !

Best Regards,
apin
0
 

Author Comment

by:pc_melsa
ID: 6228021
Dear Mr. TOndrey,
     Although the InplaceEditColor property appears in the object inspector, but the back ground color of the grid did not change to clAqua ( or any color I've selected ).

Please tell me why.

Thanks from apin
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 64
code issue 8 131
Print Graphic and Text to Epson TM-T88v 12 260
Reconfigure Delphi Install? 2 51
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

810 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