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

Borland C++ Stringgrid formatting

Status
Not open for further replies.

frankgarufi

Programmer
Apr 30, 2003
2
NG
How do I right justify when text or numbers are entered into a stringgrid cell? I'm Developing in Borland C++ Builder.
Thanks
Frank
 
I suggest you post this question on the Borland C++ Builder forum.

But if you can understand (Object) Pascal then this is how it is done in Delphi:

First you need to set the string grid DefaultDrawing property to False.

Second you need to write a handler for the string grid's OnDrawCell event.

The event handler should look something like:
Code:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  text: string;
  WidthOfText: integer;
  WidthOfCell: integer;
  LeftOffset: integer;
begin
 with StringGrid1 do begin
  text := Cells[ACol,ARow];
  WidthOfText := Canvas.TextWidth(text);
  WidthOfCell := ColWidths[ACol];
  LeftOffset := WidthOfCell - WidthOfText;
  Canvas.TextRect(Rect,Rect.Left+LeftOffset,Rect.Top,text);
 end;
end;

You may wish to enhance this code by changing the brush and pen properties of the string grid's canvas to introduce colour.

You may also wish to centre the text vertically in the cell in which case you will find the TextHeight function useful.

Andrew
 
Thanx,
I saw that yesterday but did not fully comprehend it. I took a shot at 'reinventing the wheel' by rehosting the pascal code in Borland Builder C++ and I got the following to work, if it helps someone else in the future:

void __fastcall TLocArrayParamForm::CourseArrayStringGridDrawCell(
TObject *Sender, int Col, int Row, TRect &Rect, TGridDrawState State)
{
AnsiString text;
long int WidthOfText;
long int WidthOfCell;
long int LeftOffset;

text = CourseArrayStringGrid->Cells[Col][Row];
if(Row == 0)
LeftOffset = 0; // don't right justify title row
else
{
WidthOfText=
Canvas->TextWidth(CourseArrayStringGrid->Cells[Col][Row]);
WidthOfCell= CourseArrayStringGrid->ColWidths[Col];
LeftOffset = (WidthOfCell - WidthOfText)*.9;
}
CourseArrayStringGrid->Canvas->TextRect(Rect,Rect.Left+LeftOffset,Rect.Top,text);

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top