Recent

Author Topic: Preventing TEdit from echoing some characters  (Read 5453 times)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Preventing TEdit from echoing some characters
« on: November 03, 2007, 08:48:22 am »
Using WINAPI, I can specify ES_NUMBER to an edit control to specify that the edit control can only accept numbers. I don't seem to find any way to do that in TEdit. Perhaps there should be a property called AllowedChar, which is an enum of (acAll, acAlpha, acNumber, acHex, acBin, ac...). I would like to help, but the problem is, how do I prevent the control from painting the pressed key? I've created a workaround, but it looks bad. It's something like:
Code: [Select]

procedure TMainForm.EditCtrlKeyPress(Sender: TObject; var Key: char);
var
  buf: String;
begin
  if not(Key in ['0'..'9'] then begin
    buf:=EditCtrl.Text;
    buf:=Copy(buf,1,length(buf)-1);
    EditCtrl.Text:=buf;
  end;
end;

First, it can't handle words pasted from clipboard. Second, the key will be painted first before it's deleted (I STILL CAN SEE IT BEFORE IT'S DELETED!). I need to catch the message before it's handled by the edit control. But I just don't know how.

Almindor

  • Sr. Member
  • ****
  • Posts: 412
    • http://www.astarot.tk
RE: Preventing TEdit from echoing some characters
« Reply #1 on: November 03, 2007, 10:16:02 am »
Use OnKeyDown and return Key := 0 when you don't want it to pass through. Note that it's a Word not Char so you will need to do number comparisons on VK_ eg:

if Key = VK_0 then
  Key := 0; // don't allow 0

NOTE: you need to use unit LCLType for the VK_ constants and don't forget that you need to allow also control keys (eg: arrows, backspace etc.) and numerik keyboard numbers.

Bart

  • Hero Member
  • *****
  • Posts: 5706
    • Bart en Mariska's Webstek
RE: Preventing TEdit from echoing some characters
« Reply #2 on: November 04, 2007, 12:24:12 pm »
Just for the record: in the OnKeyPress you can set Key to #0 to not pass it through to the Edit control.
Might be easier for the TS. No need to use OnKeyDown and the VK_ constants here.

You can check the OnChange of the control to eliminate pasting unallowed characters to it.
It's not too difficult.

If you want to you can derive an specialized number edit from TEdit in that way (there must be several ready made controls like that available, just google for them).
What I find difficult to implement is to set text-alignment to the right in a number editing TEDit derived control and make it platform independent (in Windows it can be done with some API call).

Bart

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
RE: Preventing TEdit from echoing some characters
« Reply #3 on: November 06, 2007, 04:07:22 am »
Bart's suggestion works as well as Almindor2, but needs less uses (no LCLType needed). For the right alignment, I think it's possible to create a derived TEdit with TAlignment as one of its properties. But we must tell that this TAlignment is used when drawing the TEdit. Should we override TControl.Invalidate?

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2674
RE: Preventing TEdit from echoing some characters
« Reply #4 on: November 06, 2007, 10:23:19 am »
overriding TControl.Invalidate won't help, TEdit is drawn by the widgetset itself. Based on the alignment flag, the widgetset classes should create the right edit.
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

 

TinyPortal © 2005-2018