Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Application wide key disable

Status
Not open for further replies.

earlrainer

Programmer
Mar 1, 2002
170
IN
I have a application (MDI application with about 50 child forms).

Now users sometimes types in characters like ' , etc which create problem when i insert data into database.

I want to prevent user from typing such characters.

One way i can do is write Onkeydown event of all forms and say key:=#0
but this is tedious task to write in all forms

is there a simple way to achieve this for example maybe write code just in the MDI main form

Thanks
 
Check/replace the critical fields in the TDataset.OnBeforeData or the equivalent event function.
(Not sure about the eventname, sorry)

HTH
TonHu
 
Hi,
set Form1.KeyPreview=True, then OnKeyPress:

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
If Key=''''
Then Key:='@';
end;

When the user strike a ' an @ appears.

Ciao,
Geppo Darkson,
 
hi Geppo,

your method would require me to write the code in all my 50 forms,
is it possible to write some code in just one place and make it applicable throughout full application?

thanks
 
Hi,
when you create your child forms, you'll probably do something like

Var F:TCustomerFrm; //
(...)
F:=TCustomerFrm.Create(Self);
F.Show;
(...)

When you use this:

Var F:TCustomerFrm; //
(...)
F:=TCustomerFrm.Create(Self);
F.KeyPreview:=True
F.OnKeyPress:=Form1.OnKeyPress;

F.Show;
(...)

You have to write it 50 times, but only in the main unit.

An elegant method is to derive a new class from TForm, override OnKeyPress method, add some Published properties (ChrFrom->ChrTo TStringList, EnableCorrector etc.) and use it as base-class for your forms
(TCustomFrm = Class(TCorrectedInputFrm) instead of TCustomFrm = Class(TForm) on the top of each unit).

This could be a nice approach to let all your forms have the same rules (for example don't close it if there are datasets in edit modes) or appearence (all this windows are elliptic) or other features like activity logging and so on.

Ciao,
Geppo Darkson.
 
Another solution you might consider:

add an event to your main form and point the Screen.OnActiveControlChange to it. the event would look something like this:

TMainForm.OnNewControl(Sender: TObject);
begin
if MDIChildCount > 0 nil then begin
if ActiveMDIChild.KeyPreview = False then
ActiveMDIChild.KeyPreview := True;
if ActiveMDIChild.OnKeyDown = nil then
ActiveMDIChild.OnKeyDown := Self.GenericKeyDown;
end;
end;

Create the GenericKeyDown event on the MainForm too and make it do what you want.

Hope this helps. (syntax not checked for errors )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top