Board index » delphi » different colours for different parts of text ?

different colours for different parts of text ?

Hi,

i have a string data in a cell of a dbgrid and i'd like to show the parts of
this string data in different colours.!

for example,
"$aKarabek, Murat"  --> this is my string data.
i'd like to show "$a" in blue and "Karabek, Murat" in red.!

Is this possible? if yes, how?

Thanks.!

 

Re:different colours for different parts of text ?


Use the OnDrawColumnCell event.  Default Drawing needs to be set to false.
Follow this example and it my get you where you want to be.

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  LeftPos : integer;
  DrawStr : string;
begin
  if ( Column.Index = 2 ) then
  begin
    LeftPos := 2;
    DrawStr := Copy( Column.Field.AsString, 1, 2 );

    with DBGrid1 do
    begin
      Canvas.Font.Color := clBlue;
      Canvas.TextOut( Rect.Left + LeftPos, Rect.Top + 2, DrawStr );

      Inc( LeftPos, Canvas.TextWidth(DrawStr) );

      DrawStr := Copy( Column.Field.AsString, 3,
Length(Column.Field.AsString) -2 );

      Canvas.Font.Color := clRed;
      Canvas.TextOut( Rect.Left + LeftPos, Rect.Top + 2, DrawStr );
    end;
  end
  else
    DBGrid1.DefaultDrawColumnCell( Rect, DataCol, Column, State );
end;

Thanks

Charles Conklin
Impact Innovations Group
cconk...@iiginc.com

Quote
Murat Karabek wrote in message <7ui4um$k...@forums.borland.com>...
>Hi,

>i have a string data in a cell of a dbgrid and i'd like to show the parts
of
>this string data in different colours.!

>for example,
>"$aKarabek, Murat"  --> this is my string data.
>i'd like to show "$a" in blue and "Karabek, Murat" in red.!

>Is this possible? if yes, how?

>Thanks.!

Other Threads