Solved

String Grid cell validation & edit mask

Posted on 2001-08-08
8
6,201 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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6366947
there you cam download the rxLibrary
http://www.rxlib.com/download.html
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now