Solved

Bold Part of a String

Posted on 2008-10-04
4
787 Views
Last Modified: 2013-11-23
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?
0
Comment
Question by:illfusion82
  • 3
4 Comments
 
LVL 14

Expert Comment

by:SteveBay
ID: 22642229
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

0
 
LVL 14

Expert Comment

by:SteveBay
ID: 22642248
Almost forgot...
You need to set DefaultDrawing := False on the DBGrid and the code above goes in a OnDrawColumnCell
0
 
LVL 1

Author Comment

by:illfusion82
ID: 22651559
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.
0
 
LVL 14

Accepted Solution

by:
SteveBay earned 500 total points
ID: 22652003
You can determine which column it is with DataSource.DataSet.Fields[DataCol].FieldName

You would need to parse out the string contained in:
DBGrid1.DataSource.DataSet.Fields[DataCol].AsString
and draw the separated parts with different fonts.

You can find the substring within a string using pos function.

You can determine the starting locations in Textout using Canvas.TextWidth.

This is mostly a new question. I would suggest posting a detailed request in a new question and perhaps I or someone else will supply detailed source.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTML-Kit Question 5 99
Java Help 2 85
AWS and JSON: Best JSON editor for creating CloudFormation Templates, etc. ? 3 111
JavaFX TableView not displaying correctly 3 18
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question