[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6727
  • Last Modified:

String Grid cell validation & edit mask

Hi, I'm trying to ensure that each cell in a string grid contains data in a valid 24hour time format.  I have a geteditmask event but this does not seem to force the user to enter four digits, they can enter less.  I also want to validate the cell to ensure that the value ranges from 0000 to 2400 - ideally I'd like to do this on a cell by cell basis as the user changes each value but I can't find an event that allows me to do this.
Any ideas please? Thanks, P

procedure TFrmOperatingTimesProfile.SGTimesGetEditMask(Sender: TObject;
  ACol, ARow: Integer; var Value: String);
begin
//set the edit mask for 24 hour time
Value :=  '!00:00;0;-';
end;
0
Pandora
Asked:
Pandora
  • 4
  • 2
  • 2
1 Solution
 
gemartiCommented:
Maybe you can get some ideas from this code:

You need a StringGrid and a Button on a form.

----------cut below line-------------


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Mask, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure StringGrid1GetEditMask(Sender: TObject; ACol, ARow: Integer;
      var Value: String);
    procedure StringGrid1Exit(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormActivate(Sender: TObject);
var
  j : integer;
  Mask : TMaskEdit;
begin
  Mask := TMaskEdit.Create(Self);
  // assign MaskEdit1 to first column of StringGrid1
      Mask.EditMask := '!90:00;1;_';
      StringGrid1.Cols[1].AddObject('Time',Mask);
  for j := 1 to StringGrid1.RowCount do
    begin
      StringGrid1.Objects[1,j] := mask;
    end;
    StringGrid1.EditorMode := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  application.Terminate;
end;

procedure TForm1.StringGrid1GetEditMask(Sender: TObject; ACol,
  ARow: Integer; var Value: String);
begin
  If StringGrid1.Cells[1,0] = 'Time' then
  Value := '!90:99;0;_';

end;

procedure TForm1.StringGrid1Exit(Sender: TObject);
var
  ARow : Integer;
begin
  if StringGrid1.Col = 1 then
    begin
      ARow := StringGrid1.Row;
      if StrToInt(StringGrid1.Cells[1,ARow]) > StrToInt('2400') then
        begin
          ShowMessage('Incorrect Time Entered');
          StringGrid1.Cells[1,ARow] := '00:00';
        end;
    end;
end;

end.


-------------Cut above Line ------------------------
0
 
kretzschmarCommented:
get the free rxlibrary,
there is a memtable-component,
use this as it is,
define there youre fields you've,
define in th onValidate-event of your fields your validation-rule,
raise an exception if the user not enters valid values,

fill in your data you've stored somewhere,
link a dataset to it,
link a dbgrid to the datasource,
for display and edit

this method is really less of selfcoding

meikl ;-)
0
 
kretzschmarCommented:
oops, typo
link a dataset to it,
should be
link a datasource to it,

:-))
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kretzschmarCommented:
there you cam download the rxLibrary
http://www.rxlib.com/download.html
0
 
PandoraAuthor Commented:
Gemarti, thanks for the sample code - I don't understand why the editmasks are added as objects when the geteditmask event already has the same effect?!  But yes I guess the  onexit event is the only way to do witout changing component it so I'll give you the points for that.  Meikl, thanks v much too - the grid is an options screen and I want to keep the app small and quick so I'm not going to monkey around with datasources they always seem to slow things down a lot, but thanks v much for the rxlib tip, I'll certainly use that next time.  Thanks alot both of you,
P.
0
 
kretzschmarCommented:
as there is no real database at backend,
it will not significant slow down your app,
and the suggestion was meant as alternative

well, if you've downloaded the rxLib
you can do some tests, like

filling 10000 rows into a stringgrid
vs
filling 10000 rows into a memtable

but its ok, gem, is more pointed to your initial q

meikl ;-)
0
 
gemartiCommented:
Thanks.

I'm beginning to dislike trying to give my interpretation of a method or event. But I think that all GetEditMask does is look at the cell (Acol,Arow) if it doesn't find a editmask it returns and empty string. The "adoption" of the object TMaskEdit places the appropriate mask in the cell prior to GetEditMask.

Just a guess....

GEM
0
 
PandoraAuthor Commented:
Meikl - thanks yes, for biggies I'll certainly have a play and thankyou- the max no of records for the Sgrid I'm using is 7!  Hence, just wanting something quick & dirty!
Gem, geteditmask allows you to set an edit mask directly by setting the 'value' var (hence the duplication I was asking about, as your code does this).  But thanks for help anyway, P :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now