Link to home
Start Free TrialLog in
Avatar of illfusion82
illfusion82

asked on

Bold Part of a String

I have a DBGrid1 and I am trying to bold part of a string in a field. For example I have HelloWorld, and I want to bold out the Hello part of it and leave the world part normal. How would I do this?
Avatar of SteveBay
SteveBay
Flag of United States of America image

Here is an example of how to Bold the First Letter of each cell. You should be able to take this logic and expand it to match your needs
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
    With Sender As TDBGrid Do
      Begin
      Canvas.FillRect(Rect) ;
      Canvas.Font.Style := Canvas.Font.Style - [fsBold];
      Canvas.TextOut(Rect.Left + Font.Size , Rect.Top + 2 , DBGrid1.DataSource.DataSet.Fields[DataCol].AsString ) ;
      Canvas.Font.Style := Canvas.Font.Style + [fsBold];
      Canvas.TextOut(Rect.Left + Font.Size , Rect.Top + 2 , DBGrid1.DataSource.DataSet.Fields[DataCol].AsString[1] ) ;
      End ;
end;

Open in new window

Almost forgot...
You need to set DefaultDrawing := False on the DBGrid and the code above goes in a OnDrawColumnCell
Avatar of illfusion82
illfusion82

ASKER

Hello SteveBay. Thanks for the code. Now is there a way to type in an TEdit field and have the letters I am typing in the TEdit field to bold out the letters in a single column. The code you gave me bolds out the first character in all columns. Kind of like the way Mozilla firefox uses autocomplete in their address field. When a user types in Mozilla Firefoxes address field it automatically bolds the letters that are being typed in the address field.
ASKER CERTIFIED SOLUTION
Avatar of SteveBay
SteveBay
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial