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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resusing code for different controls 1

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
I have three StringGrids (sgInvoice, sgPreview, sgUpdate)and am using the same OnDrawCell event for each StringGrid. Can I possibly have one block of code which is used for the OnDrawCell event for all the StringGrids?

Here is my code for the OnDrawCell event. It is identical to the other StringGrids (sgPreview, sgUpdate).

Thanks in advance.

Code:
procedure TfrmMain.sgInvoiceDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  S: String;
  drawrect :trect;
begin
  //paint column backcolour
  Case ACol of
    3:
      begin
      sgInvoice.Canvas.Brush.Color := clCream; //clBtnFace;
      sgInvoice.Canvas.FillRect(Rect);
      sginvoice.Canvas.Font.Color := clBlack;
      sgInvoice.Canvas.TextOut(Rect.Left + 2 , Rect.Top + 2, sgInvoice.Cells[ACol, ARow]);
      end;
    4:
      begin
      sgInvoice.Canvas.Brush.Color := clAppWorkSpace;
      sgInvoice.Canvas.FillRect(Rect);
      sginvoice.Canvas.Font.Color := clWhite;
      sgInvoice.Canvas.TextOut(Rect.Left + 2 , Rect.Top + 2, sgInvoice.Cells[ACol, ARow]);
      end;
    else
      begin
      sgInvoice.Canvas.Brush.Color := clWindow;
      sgInvoice.Canvas.FillRect(Rect);
      sgInvoice.Canvas.Pen.Color := clBlack;
      sgInvoice.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, sgInvoice.Cells[ACol, ARow]);
      end;
  end; {case}

  //create a word break if a line exceeds column width
  S:= sgInvoice.Cells[ACol, ARow ];
  If Length(S) > 0 Then
    begin
      drawrect := rect;
      DrawText(sgInvoice.canvas.handle,
               Pchar(S), Length(S), drawrect,
               dt_calcrect or dt_wordbreak); // or dt_left );
    If (drawrect.bottom - drawrect.top) >
       (sgInvoice.RowHeights[ARow]) then
      sgInvoice.RowHeights[ARow] := (drawrect.bottom - drawrect.top)
    // changing the row height fires the event again
    else
      begin
        drawrect.Right := rect.right;
        drawrect.Left := rect.Left + 2;
        drawrect.Top := rect.Top + 2;
        sgInvoice.canvas.fillrect( drawrect );
        DrawText(sgInvoice.canvas.handle,
                 Pchar(S), Length(S), drawrect,
                 dt_wordbreak); // or dt_left);
      end;
  end;
end;




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
you can replace sgInvoice and make it more general like this

Code:
var sgTemp : TStringGrid;

begin
 sgTemp:=TStringGrid(Sender);
 ... // replace all SgInvoice occurences by sgTemp
end;

this way, you can have 1 procedure for all stringgrids

--------------------------------------
What You See Is What You Get
 
I didn't quite catch what you mean I delete all the DrawCell events (leaving only the DrawCell event for for sgInvoice) only, then pass the StringGrid as Sender (for sgPreview & sgUpdate)? I am confused.






------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
like this :

Code:
procedure TfrmMain.DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  S: String;
  drawrect :trect;
  sgTemp : TStringGrid;
  
begin
 sgTemp:=TStringGrid(Sender);
  //paint column backcolour
  Case ACol of
    3:
      begin
      sgTemp.Canvas.Brush.Color := clCream; //clBtnFace;
      sgTemp.Canvas.FillRect(Rect);
      sgTemp.Canvas.Font.Color := clBlack;
      sgTemp.Canvas.TextOut(Rect.Left + 2 , Rect.Top + 2, sgTemp.Cells[ACol, ARow]);
      end;
    4:
      begin
      sgTemp.Canvas.Brush.Color := clAppWorkSpace;
      sgTemp.Canvas.FillRect(Rect);
      sgTemp.Canvas.Font.Color := clWhite;
      sgTemp.Canvas.TextOut(Rect.Left + 2 , Rect.Top + 2, sgTemp.Cells[ACol, ARow]);
      end;
    else
      begin
      sgTemp.Canvas.Brush.Color := clWindow;
      sgTemp.Canvas.FillRect(Rect);
      sgTemp.Canvas.Pen.Color := clBlack;
      sgTemp.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, sgTemp.Cells[ACol, ARow]);
      end;
  end; {case}

  //create a word break if a line exceeds column width
  S:= sgTemp.Cells[ACol, ARow ];
  If Length(S) > 0 Then
    begin
      drawrect := rect;
      DrawText(sgTemp.canvas.handle,
               Pchar(S), Length(S), drawrect,
               dt_calcrect or dt_wordbreak); // or dt_left );
    If (drawrect.bottom - drawrect.top) >
       (sgTemp.RowHeights[ARow]) then
      sgTemp.RowHeights[ARow] := (drawrect.bottom - drawrect.top)
    // changing the row height fires the event again
    else
      begin
        drawrect.Right := rect.right;
        drawrect.Left := rect.Left + 2;
        drawrect.Top := rect.Top + 2;
        sgTemp.canvas.fillrect( drawrect );
        DrawText(sgTemp.canvas.handle,
                 Pchar(S), Length(S), drawrect,
                 dt_wordbreak); // or dt_left);
      end;
  end;
end;

assign this procedure to the ondrawcell event of all three stringgrids.

--------------------------------------
What You See Is What You Get
 
Thanks, one last thing...

How will this work? If I do this, wont I have to pass the parameters for:

Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState

Shouldn't the procedure be:

Code:
procedure TfrmMain.DrawCell(Sender: TObject);

Then on the DrawCell event:

Code:
procedure TfrmMain.sgInvoiceDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  Sender := sgInvoice;
  DrawCell(Sender);





------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
no, normally you can assign my Drawcell procedure with the object inspector. (click events tab and then click on the dropdown next to the ondrawcell event)

--------------------------------------
What You See Is What You Get
 
OK works fine, but one stringgrid is on another form. How can I get around this?




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

Part and Inventory Search

Sponsor

Back
Top