Interesting and useful Delphi components - TAdvStringGrid

Today we're about to discuss some special features of TMS String grid component - great and featured-rich component based on original Embarcadero StringGrid. TMS shifted using the string grids to superb level and actually we'd look hardly for feature which is not exposed in TAdvStringGrid.

As component name says, string grid is primarily inteded for handling strings - entering, displaying, sorting, filtering, exporting or printing. Following article describes less common working with strings by means of HTML.

As a matter of fact, common and well-known HTML tags can be used inside cells, like tags for emphasizing text (BOLD, UNDERLINE), using anchors, creating numbered or unnumbered lists in cells, formatting cell text (break, paragraph, horizontal lines), highlighting etc.

However, TAdvStringGrid allows using so called Mini HTML forms to place new components and/or their combinations into selected cells.

Mini HTML forms inside TAdvStringGrid cells

TMS AdvStringGrid Developer's guide provides some technical information about using Mini HTMl forms:

Combining multiple buttons in a cell, adding more than one checkbox in a cell, editing different items in a cell, it is possible with TAdvStringGrid and its mini HTML forms. Mini HTML forms bring a solution allowing unlimited capabilities to specify cell contents and behaviour. Controls can be specified through a the tag CONTROL The CONTROL tag takes following parameters (the whole string must be placed between "<>" chars):

CONTROL ID="ControlID" VALUE="ControlValue" TYPE="ControlType" WIDTH="ControlWidth" MAXLEN=”ControlMaxLenValue” 
  • ControlID = unique ID string per cell for the control
  • ControlType = "EDIT" or "CHECK" or "RADIO" or "COMBO" or "BUTTON"
  • ControlWidth = width of the control in pixels
  • ControlValue = value of the control depending on the type
  • ControlMaxLenValue = optional maximum edit length of edit control. When MAXLEN attribute is not specified or value of ControlMaxLenValue is 0, string length is not limited.
  • "TRUE", "FALSE" for checkboxes and radiobuttons Button caption for button control Text value for edit and combobox controls

Result can be seen on the picture below - please keep on mind that we do not look at the form but the grid where particular cells contain edit boxes, radiobuttons, checkboxes or buttons.


Using Mini HTML form in real life

Our company deals with creating various applications for learning, among another things, and such apps contain tests in very variable shapes. We have always to keep legislative requirements like length of questions, number of answers, unique or multiple-choice answers, images close-knit to the question and so on. Such as learning app for applicants for dispatch service licenses offers to pass a case study because our law requires it. In addition, case study must consists of three types of questions:

  • unique choice answer
  • multiple choice answer
  • written text

Customization of requested cells for entering written text is very simple and it can be done through property DefaultEditor. For placing radiobuttons into the given cell we need some easy coding:

with AdvStringGrid1 do
  cells[2,i]:=cells[2,i]+'<'+'CONTROL TYPE="RADIO" WIDTH="15" ID="RB1">A
'; cells[2,i]:=cells[2,i]+'<'+'CONTROL TYPE="RADIO" WIDTH="15" ID="RB2">B
'; cells[2,i]:=cells[2,i]+'<'+'CONTROL TYPE="RADIO" WIDTH="15" ID="RB3">C
'; cells[2,i]:=cells[2,i]+'<'+'CONTROL TYPE="RADIO" WIDTH="15" ID="RB4">D
'; end;

Code mentioned above creates four radiobuttons in given cell labelled A, B, C and D as a choices for user's answer. Practically, grid looks like this:


Similar steps are necessary when we need to have certain figure of checkboxes in the cell. Syntax is almost the same, only difference is using CHECK control type instead of RADIO:

with AdvStringGrid1 do
  for j:=1 to strtoint(RMG1.cells[2,1]) do
    case j of
      1:cells[2,i]:=cells[2,i]+'<'+'CONTROL TYPE="CHECK" WIDTH="20" ID="CK'+inttostr(j)+'">A
'; 2:cells[2,i]:=cells[2,i]+'<'+'CONTROL TYPE="CHECK" WIDTH="20" ID="CK'+inttostr(j)+'">B
'; 3:cells[2,i]:=cells[2,i]+'<'+'CONTROL TYPE="CHECK" WIDTH="20" ID="CK'+inttostr(j)+'">C
'; 4:cells[2,i]:=cells[2,i]+'<'+'CONTROL TYPE="CHECK" WIDTH="20" ID="CK'+inttostr(j)+'">D
'; 5:cells[2,i]:=cells[2,i]+'<'+'CONTROL TYPE="CHECK" WIDTH="20" ID="CK'+inttostr(j)+'">E
'; 6:cells[2,i]:=cells[2,i]+'<'+'CONTROL TYPE="CHECK" WIDTH="20" ID="CK'+inttostr(j)+'">F
'; 7:cells[2,i]:=cells[2,i]+'<'+'CONTROL TYPE="CHECK" WIDTH="20" ID="CK'+inttostr(j)+'">G
'; 8:cells[2,i]:=cells[2,i]+'<'+'CONTROL TYPE="CHECK" WIDTH="20" ID="CK'+inttostr(j)+'">H
'; 9:cells[2,i]:=cells[2,i]+'<'+'CONTROL TYPE="CHECK" WIDTH="20" ID="CK'+inttostr(j)+'">I
'; end; {CASE} end; end;

Setting of control's IDs is done by FOR loop where particular controls are identified as CK1, CK2, CK3 and so on. We need to determine each checkbox because of consequent analysis what checkboxes were checked by user during the test.

How to obtain state of controls in Mini HTML forms

Getting information about Mini HTML forms controls is not complicated and we should use combination of ControlValues property and control's ID. Following piece of code shows how to check which answer (A, B, C, D) was selected from four radiobuttons set:

if controlvalues[2,i,'RB1']='TRUE' then cvys:='A';
if controlvalues[2,i,'RB2']='TRUE' then cvys:='B';
if controlvalues[2,i,'RB3']='TRUE' then cvys:='C';
if controlvalues[2,i,'RB4']='TRUE' then cvys:='D';

As one can see, TMS AdvStringGrid can be used not only for common string handling tasks but even for nontraditional purposes. Finally, indication and navigation bar in the header of the test window at the picture below (higlighted green) is AdvStringGrid too:-)


About the Author

Ing. Karel Janeček, MBA, MSc.