Author: Alex Schlecht
String-Grids are very usefull, but sometimes it's necessary to use an own
Inplace-Editor. For example to make a Grid which will allow only numbers but no
Text-Characters.
Answer:
When you are using Grids (TStringGrid, TDBGrid), you can input some text in the
cells of the grid. This will be done with the "Inplace-Editor" from Borland.
Sometimes it's necessary to make an own Inplace-Editor, for example to prevent the
user to give in Text instead of number. The following example shows how to do this.
First you need two new classes: one for your Grid and one for your Inplace-Editor.
In this example I use TStringGrid, but it should also work with TDBStringGrid.
1 unit u_TMyStringGrid;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 Grids;
8
9 type
10
11 // My own Inplace-Editor. This Editor -for example- only
12 // allow numbers, no text
13 TMyInplaceEdit = class(TInplaceEdit)
14 protected
15 procedure KeyPress(var Key: Char); override;
16 end;
17
18 // My own StringGrid, which will use my own Inplace-Editor
19 TMyStringGrid = class(TStringGrid)
20 protected
21 function CreateEditor: TInplaceEdit; override;
22 end;
23
24 implementation
25
26 { TMyStringGrid }
27 // Here i define, that my StringGrid should use MyInplace-Editor
28
29 function TMyStringGrid.CreateEditor: TInplaceEdit;
30 begin
31 Result := TMyInplaceEdit.Create(Self);
32 end;
33
34 { TMyInplaceEdit }
35 //The Inplace-Edit only allowes numers, no text-Characters
36
37 procedure TMyInplaceEdit.KeyPress(var Key: Char);
38 begin
39 if not (Key in ['0'..'9']) then
40 begin
41 beep;
42 Key := #0
43 end
44 else
45 inherited;
46 end;
47
48 end.
|