Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

The brush color in  DBGrid

Posted on 2001-06-22
7
Medium Priority
?
604 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
[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
  • 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 300 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

722 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