Grid Word Highlighting

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]




Search WWW


As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 


We receive a few cents from each purchase.  Thanks


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search only




Problem Description

Here's a "proof of concept" demo program implementing my ideas for highlighting specific words in a StringGrid or DBGrid.

Background & Techniques

A fellow Delphian recently asked if it was possible to highlight a particular word in a DBGrid. I gave him some suggestions but didn't hear, back so I decided to try it for myself.

I used a StringGrid for this demo. The relevant feature exists in both -  the ability to control the drawing of each cell.  I have provided a sample phrase list from which grid cells are randomly selected. There's also an edit box for entry of  the word or phrase to highlight. This allows testing the word to highlight in various locations, capitalization, delimiters, etc. Buttons to save and load new Master phrase lists and a "Refill Grid" button will load a new set of randomly selected phrases to the grid.

Highlight formatting choices can change the length of the text display, e.g. font size or style. For demonstration purposes, I chose font color of Red with Bold style for the highlighted text.  Column width is automatically increased if the displayed text exceeds the current width.

Additional options such as highlight text embedded in other words, highlighting multiple occurrences within a phrase,  or requiring case matching were not implemented but could be.   As a cell is drawn we look for the "highlight  word" within the cell without worrying about case matching or word delimiters. When a match is found, then we test that the text is surrounded by delimiters. This allows single words or phrases with other embedded delimiters to be entered into the Highlight text box for highlighting.

Notes for programmers

Tricky items:

bulletInitialize grid canvas font:  I used the FormCreate event exit assign the grid font to the Canvas,Font property.  The OnDrawCell event exit procedure draws on the canvas and uses its font.  Temporary changes are made there for highlighting.      
bulletHighlight in three pieces: The current Highlight word is used to search the cell contents before redrawing each  cell.   This implementation ignores text case and ignores embedded occurrences by requiring a delimiter at each end unless the word begins or ends the cell text.  If found, the text is written in three pieces. Beginning text + Highlight text + trailing text,  modifying the font properties for the highlighted piece.
bulletAllow applying highlight word entry by mouse click or Enter key: We use the OnClick and OnKeyPress event exits to allow either either mouse clicks or pressing the Enter key to  redraw the current FillWordEdt text box.   Either event triggers a call to the FillWordEdtClick procedure.
bulletAdjust column width after drawing if necessary:  If the highlighting increases the pixel width of a cell, has we want to increase the width of the column containing that cell if it exceeds the current width.  The Drawcell procedure can measure this new width, but we can't actually increase the width here since that will trigger redrawing - a bad thing to do during the deawing process.  Instead, a MaxColWidths array is set with required column width and the FillWordEdtClick procedure OnChange procedure updates the column widths from MaxColWidths array after the new highlight word has been applied.
bulletTrack changes to master phrase list:  It's always good  to remind user if they are about to lose changes by loading a new list or closing the program.  A protocol I've often used, including in this demo, defines a Modified Boolean variable which is initialized as False and set to True by the OnChange event exit for the FillWordsMemo display.   A ChkModified function checks the Modified variable and offers the user a chance to save the contents if it is True. ChkModifed return True if no change is required or the user replies No to the save requestThe SaveBtnClick method resets Modified to False after the save.   LoadBtnClick and OnCloseQuery mehthod both call ChkModified.

March 15, 2018: A viewer recently reported that the demo used the OnSetEditText event exit which is not available when using a DBGrid instead of the Stringgrid used in the demo.  Today's posting removed the redundant event exit routine.  It originally was used to expand column widths when highlighting caused text in a cell to exceed the viewable width.  A MaxColumnWidth array now tracks width needed when text is highlighted and the OnDrawCell exit expands column width from that array when necessary.    

Running/Exploring the Program 

bulletDownload executable
bullet Download source   

Suggestions for Further Explorations

Options to flag embedded target words, multiple occurrences within a cell and text case matching. 

Created: July 19, 2015

Modified: May 15, 2018

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2018, Gary Darby    All rights reserved.