Solved

The brush color in  DBGrid

Posted on 2001-06-22
7
544 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

18 Experts available now in Live!

Get 1:1 Help Now