Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple procedures - one unit 2

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
Hi,

I have a the exact same DrawCell procedure for two StringGrids on two different forms (about 65 lines of code). Is it possible to make a separate unit containing the DrawCell procedure, which can then be used by both forms for both StringGrids? Is this a better way of coding it?

Thanks in advance.




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
I suppose it depends on you definition of 'better'

This wont be easy as you cannot make ondrawcell untyped i.e it must decend from a Tstringgrid object to access TGriddrawstate, it also needs to access TRect.
Normally it 'belongs' to the calling stringgrid, you would have to create it as a typed procedure and then cast it to the calling grid.

If there are only two instances I would keep them seperate.


Steve:
A Delphi Programmer
A Feersum Endjinn indeed
 
Hi.
Yep, it's possible.
You have to redirect the event by using code.
Name 2 forms Form1 and Form2 and the grids to SrcGrid (Form1-Grid), DestGrid (Form2-Grid)

Form1.SecGrid has the original DrawCell procedure.
On Form2 constructor, you add following code:
DestGrid.OnDrawCell = Form1.SrcGrid.OnDrawCell or
DestGrid.OnDrawCell = Form1.SrcGridDrawCell




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is always another way to solve it, but I prefer my way.
 
You asked for the DrawCell to be put in a separate unit. Nordlund's reply doesn't actually do that.

Your separate unit should look something like:-
Code:
unit DrawCell;

interface

uses Types, Grids;

procedure GenericDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);

implementation

procedure GenericDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  (Sender as TStringGrid).Canvas.TextRect ( rect, rect.Left, rect.top, 'TEST' );
end;

end.
Obviously this code simply writes the string TEST in each cell. Yours should include your 65 lines of common code.

In each of the forms that want to call this procedure, the OnDrawCell event handlers should look like:
Code:
Uses DrawCell;
...
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  GenericDrawCell ( sender, acol, arow, rect, state );
end;
This will give you the ability to put some code before the call to GenericDrawCell if you wish. You might want to set a different cell color for each of your forms before calling GenericDrawCell. For example:-
Code:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  StringGrid1.Canvas.Brush.Color := clRed;
  GenericDrawCell ( sender, acol, arow, rect, state );
end;

Andrew
Hampshire, UK
 
Thanks heaps guys! That has really helped me out.




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top