Solved

String Grid cell validation & edit mask

Posted on 2001-08-08
8
6,350 Views
Last Modified: 2012-08-13
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
Comment
Question by:Pandora
  • 4
  • 2
  • 2
8 Comments
 
LVL 21

Accepted Solution

by:
gemarti earned 100 total points
ID: 6366590
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6366936
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6366939
oops, typo
link a dataset to it,
should be
link a datasource to it,

:-))
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6366947
there you cam download the rxLibrary
http://www.rxlib.com/download.html
0
 
LVL 1

Author Comment

by:Pandora
ID: 6367543
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6367638
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
 
LVL 21

Expert Comment

by:gemarti
ID: 6367745
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
 
LVL 1

Author Comment

by:Pandora
ID: 6368460
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question