Solved

String Grid cell validation & edit mask

Posted on 2001-08-08
8
6,297 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 151
LAN or WAN ? 11 90
Create a path if not exists 7 77
Breakpoint doesn't stop in my variable 3 23
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

776 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