Solved

Handling key presses on a TEdit

Posted on 2008-10-25
5
215 Views
Last Modified: 2013-11-23
Hi all,

Just a question I can't figure out.

I've got a TEdit which must handle the - and + of the numeric keypad, but not the - and + of the alphanumeri part of the keyboard.
When I put a break point on the OnKeyDown event I see that it makes a difference on the numeric and alphanumeric part of the - sign (#189 and #109).

But how can I code that when the subtract key is pressed on the numeric keypad that it will not put the - in the TEdit box and will do when the alphanumeric - is pressed?

Thanks in advance,

JeePeeTee
0
Comment
Question by:JeePeeTee
  • 3
  • 2
5 Comments
 
LVL 28

Accepted Solution

by:
ciuly earned 250 total points
Comment Utility
something like this?
unit Unit1;
 

interface
 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;
 

type

  TForm1 = class(TForm)

    Edit1: TEdit;

    procedure Edit1KeyPress(Sender: TObject; var Key: Char);

    procedure Edit1KeyDown(Sender: TObject; var Key: Word;

      Shift: TShiftState);

  private

    { Private declarations }

    pressed:word;

  public

    { Public declarations }

  end;
 

var

  Form1: TForm1;
 

implementation
 

{$R *.dfm}
 

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);

begin

  if (key='-') and (pressed=109) then

    key:=#0;

end;
 

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;

  Shift: TShiftState);

begin

  pressed:=key;

end;
 

end.

Open in new window

0
 
LVL 4

Author Comment

by:JeePeeTee
Comment Utility
Thanks,

Kinda weird that you can't do it without declaring a pricate symbol.
But ah, well, it works!

0
 
LVL 4

Author Closing Comment

by:JeePeeTee
Comment Utility
It works, though it's weird that it must be done with a private symbol.
0
 
LVL 28

Expert Comment

by:ciuly
Comment Utility
Why a B grade? see grading tips here: http://www.experts-exchange.com/help.jsp#hi403
0
 
LVL 4

Author Comment

by:JeePeeTee
Comment Utility
It's a B-grade because the solution is good, but not excellent. Like I mentioned, the solution works, but I think it's kinda weird that I need a private symbol to pass trhough two seprate OnKey events.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now