MSTRGRID

Version 2.1,  11.9.1996,  PostCardWare, Albrecht Mengel, mengel@stat-econ.uni-kiel.de

If you have problems/ideas with mStrGrid or mStrList, please feel free to email me.

Bugfix in 2.1: Method SortCol

New in 2.0: Sort Date

New in 1.3: Properties ReverseSearch, SearchExists, SortIndex
            Method UndoSort

New in 1.2: Click Event is called when clicking on fixed rows/cols
            AND property ClickFixed is true.
            SortDescending: new property

New in 1.1: Cut/Copy/Paste with Clipboard (Works with Excel).
            EnableClipboardShortcuts and PasteToCursor properties

New Tip: You can easy use the LoadFromFile/SaveToFile methods with #9 as delimiter.
         This is useful with Excel.

This is a component descending from TStringGrid with some new sorting and searching properties & methods:

************************************
* Inserting / Deleting / Modifying *
************************************

procedure InsertCols(where,howmuch:Integer);
procedure InsertRows(where,howmuch:Integer);
procedure DeleteCols(where,howmuch:Integer);
procedure DeleteRows(where,howmuch:Integer);
   It does not matter if there are less rows/cols than you want to delete.
procedure AddCol(contents:String;  delimiter:Char);
procedure AddRow(contents:String;  delimiter:Char);
   The contents are splitted into the single cells by the delimiter
function ModifyRow(which:integer; contents,delimiter:string):integer;
   Results in the number of columns
function ModifyCol(which:integer; contents,delimiter:string):integer;
   Results in the number of rows

***********************
* Sorting / Searching *
***********************

The following properties control sorting and searching:

property KeyType:(soString,soStringCaseSensitive,soNumeric,soDate)
   This is the kind, how the keys (and cells) are compared.
   If you work with soNumeric, all non numbers get the same value 0.
      As these zero values would flip in random order a (case insensitive) string sort
      is performed after. So, first come the negatives, then the strings, and thereafter
      the positives.
   Date sorting works with the Delphi StrToDateTime function. It should work with your own
      date format you use (See the Delphi help for the global date format Delphi uses)
property KeyPos:Integer;
property KeyLen:Integer;
   Here you may define, which substring of the cells is used to comparision.
   (Default is KeyPos=1 & KeyLen=MaxInt)
property UseFixed:(hfNot,hfSmart,hfYes);
   hfNot: The fixed rows/columns remain as they are (and are not used in searching)
   hfSmart: When sorting complete rows, the fixed columns are part of the rows and
            change their contents in the same manner as the rows are exchanged.
            The top row (fixed rows) remain intact.
            When sorting complete columns, the fixed rows change and the fixed columns not.
            When sorting (single) row/column, hfSmart protects the fixed part of the row/column.
   hfYes: All sorting includes the fixed columns and rows (as if they were set to 0)
property SortDescending:Boolean;
   false (default) : Ascending sorting order
   true            : Descending sorting order

***********
* Sorting *
***********

procedure SortCompleteColumns(KeyRow:integer);
procedure SortCompleteRows(KeyCol:integer);
procedure SortRow(ThisRow:integer);
procedure SortCol(ThisCol:integer);
procedure SortAllRows;
   The rows are sorted independand
procedure SortAllCols;
   The columns are sorted independand

procedure UndoSort;
  This re-sorts the last sorting.
  Multiple calls of UndoSort have only effect on the last sorting scheme:
   UndoSort;UndoSort -> the sorted order remains.
property SortIndex:TmStrList
  Here you see the actual index. That are the positions, the contents have to be moved
  from, when the undo actions should be made.


*************
* Searching *
*************

Searching can be done with the fixed substrings of cells
   (set SearchSubstring=true and use KeyPos and KeyLen) or
   anywhere in the cells (SearchSubstring=false)
property SearchSubstring:Boolean;

function FindFirst(Key:String; RowWise:Boolean; VAR ResultCol,ResultRow:Integer):Boolean;
    Searches all cells, if UseFixed=hfYes or excludes the fixed cells, if UseFixed=hfNo.
    If UseFixed=hfSmart, then the fixed cols are used if searching rowwise or
                              the fixed rows are used if not searching rowwise.
    All finding methods result in true, when a cell was found.
    If nothing found then ResultCol and ResultRow are set to -1.
function FindFirstInRow(Key:String; searchRow:Integer; VAR ResultCol,ResultRow:Integer):Boolean;
    The same as FindFirst, but only one row is used for searching.
    UseFixed=hfSmart is here the same as hfNo.
function FindFirstInCol(Key:String; searchCol:Integer; VAR ResultCol,ResultRow:Integer):Boolean;
function FindNext(VAR ResultCol,ResultRow:Integer):Boolean;

One can invert the searching direction by the property ReverseSearch. If set to true, then the search
starts at the end. The property may be changed while a search is not ready.
The readonly property SearchExists is set true, if any search was started (and then remains true).
If a search is ready (results in -1,-1) you may invert ReverseSearch. Then the search is started
again at the actual end in the opposite direction and you can get the first result by a FindNext.

A sample searching program:
 if FindFirst('steven',true{rowwise},c,r) then
 repeat memo1.lines.add('Found in cells['+inttostr(c)+','+inttostr(r)+'] = "'+cells[c,r]+'"');
 until not FindNext(c,r);


*******************
* Import / Export *
*******************

File can be read to or written from the table as ASCII file.
The second parameter is the column delimiter in every row.
    procedure LoadFromFile(FileName,ColSep:string);
    procedure SaveToFile(FileName,ColSep:String);

The contents of the selected area can be handled with the clipboard:
    procedure CopyToClipboard;
    procedure CutToClipboard;
              Like CopyToClipboard, but empties then selected cells
    procedure PasteFromClipboard;
              Replaces the selected cell rectangle.
              When PasteToCursor is false (default setting),
              not the cursor position is relevant, but the upper left
              corner of the selected area.
    property  PasteToCursor: controls the PasteFromClipboard method:
              When true, the cells are inserted at cursor position.
 The internal format is Tab Delimited Text.

If the property EnableClipboardShortcuts is set to true, and when NOT editing,
you can perform CopyToClipboard by Ctrl-C, CutToClipboard by Ctrl-X,
and PasteFromClipboard by Ctrl-V.

If the property ClickFixed is true the Click Event is called when clicking on fixed rows/cols.
  Col and Row are set to the proper values.
  The implementation is difficult and not yet flickerfree. (Description in the heading of mStrGid.pas)
  As a result of the implementation the OnClick Procedure reads the FixedCols and FixedRows
  values always as zero (They are temporarily set so). This may give some strange behaviour
  in your program - just use SavedFixedCols and SavedFixedRows in the OnClick procedure.
  Also, if clicked the second time at exact the same mouse position in a short time,
  it does not call the click routine. Reason: Your Doubleclick is set too long!
  Remember: property ClickFixed MUST be true.

********
* Demo *
********

The demo program has one odd property: if found something in the fixed parts of the grid,
the fixed rows/cols are duplicated. (Because it is not allowed to set the cursor there to)

The file mStrList is required for compilation.
It contains a 'sister' of TStringList, which can sort in the above manner and is a complete copy
of TStringList, which can be found in \source\vcl\GRIDS.PAS.
But I had a problem with compiling it completely.
If you are good in programming OOP / VCL then please have a look in mStrList.pas and
help me fixing the problem. Until now all seems to work.

To install mStrGrid:

1. Copy mStrGrid.* and mStrList.* to a directory for your special delphi components.
2. Select the Component: Install menu.
3. Click the Add button.
4. Click the Browse button.
5. Locate and select the mStrGrid.pas file
6. Click the Ok buttons to close all the dialogs.
The Delphi component library DLL will be recompiled with the new mStrGrid component
added to the Additional page of the component palette.

Software development:

Programming consumes time, and programmed components save time.
If you like my components feel free to send me some acknowledgment.
I accept post cards of your town, money or cheques (2$ up to 20$),
if you use mStrGrid for private.
For commercial use I give this code free for $20 (or more).

This is a motivation for me to continue developing for you.

If you have some ideas to improve mStrList, mStrGrid or any other component
send me a message.

The component is copyright (C) 1996, by Albrecht Mengel. You may give copies to
others by copying the original, unmodified zip file. You may use this component
in your own projects free of charge as long as those projects are public domain,
freeware or shareware project.

The author of mStrList and mStrGrid (A. Mengel) makes no warranty of any kind,
expressed or implied, including without limitation any warranties of merchantability
and/or fitness for a particular purpose. In no event will the author be liable to you
for any additional damages, including any lost profits, lost savings, or other
incidental or consequential damages arising from the use of, or inability to use,
this software and its accompanying documentation, even if the author has been advised
of the possibility of such damages.

Albrecht Mengel, University of Kiel, Germany
Institute for Statistics & Economics
Olshausenstrasse 40-60,
D-24098 Kiel
Tel. +49-431-880-2424
Fax. +49-431-880-2673
Email: mengel@stat-econ.uni-kiel.de
http://www.stat-econ.uni-kiel.de/pers/mengel.htm
