Override default KeyPress method in TEdit class in StdCntrls unit
To avoid entering single quotes. But got error wh开发者_运维问答ile compiling the project:
[Fatal Error] StdCtrls.pas(1238): Unit Dialogs was compiled with a different version of StdCtrls.TEdit
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:
- Override
KeyPressto reject unwanted keys. Splash's answer demonstrates that. - Provide a
wm_Pastemessage handler to filter unwanted characters from the clipboard. - Provide
wm_SetTextandem_ReplaceSelmessage handlers to filter unwanted characters arising from direct use of theTextandSelTextproperties.
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;
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.
加载中,请稍侯......
精彩评论