melvina
melvina

Reputation: 11

Override default KeyPress method in TEdit class in StdCntrls unit

To avoid entering single quotes. But got error while compiling the project:

[Fatal Error] StdCtrls.pas(1238): Unit Dialogs was compiled with a different version of StdCtrls.TEdit

Upvotes: 1

Views: 2692

Answers (3)

splash
splash

Reputation: 13327

Simply write an OnKeyPress event handler:

procedure TMyForm.EditNoSingleQuotes(Sender: TObject; var Key: Char);
begin
  if Key = '''' then Key := #0;
end;

Or inherit from TEdit and override the KeyPress method:

procedure TMyEdit.KeyPress(var Key: Char);
begin
  if Key = '''' then Key := #0;
  inherited KeyPress(Key);
end;

Upvotes: 3

Rob Kennedy
Rob Kennedy

Reputation: 163287

You changed the interface of the StdCtrls unit. That requires all units that use it to be recompiled as well, even the Delphi-provided VCL units. If there's ever a way to accomplish a goal without modifying Delphi's units, prefer it.

There's no need to provide your own version of StdCtrls.pas. Everything you need to do can be done by inheriting from the basic TEdit control. Years ago, Peter Below demonstrated how to filter the input of an edit control to accept only numbers. You can adapt that code to accept everything except apostrophes.

In short, you do this:

Upvotes: 8

Otherside
Otherside

Reputation: 2855

The source code for the VCL is available to read and to debug but the license does not allow you to make changes and distribute those changes (at least as far as I know).

In your case it would be better to make a new control that descends from TEdit (or TCustomEdit) if you want to reuse this control in multiple forms or projects.

Upvotes: 2

Related Questions