執行這些代碼, Edit1只能輸入數字,小數點和負號,負號和小數點只能輸入一個,負號必須在最前,粘貼的數字必須完全正確.
阿新 • • 發佈:2017-05-20
eat win method and length use double .text sel
執行這些代碼, Edit1只能輸入數字,小數點和負號,負號和小數點只能輸入一個,負號必須在最前,粘貼的數字必須完全正確. type TForm1 = class(TForm) Edit1: TEdit; procedure FormCreate(Sender: TObject); private Edit1OldProc: TWndMethod; procedure Edit1NewProc(var Message: TMessage); public { Public declarations } end;var Form1: TForm1; implementation {$R *.dfm} uses Clipbrd; procedure TForm1.FormCreate(Sender: TObject); begin Edit1OldProc := Edit1.WindowProc; Edit1.WindowProc := Edit1NewProc; end; procedure TForm1.Edit1NewProc(var Message: TMessage); var s: string; v: double; begincase Message.Msg of WM_PASTE: begin s := Clipboard.astext; s := copy(edit1.Text, 0, edit1.SelStart) + s + copy(edit1.Text, edit1.SelStart + edit1.SelLength, length(edit1.Text)); if trystrtofloat(s, v) then Edit1OldProc(Message) else ShowMessage(‘只允許輸入數字‘); end; WM_CHAR: begin if (TWMCHAR(Message).CharCode = 45) then if (Pos(‘-‘, Edit1.Text) = 0) and (Edit1.SelStart = 0) then Edit1OldProc(Message); if (TWMCHAR(Message).CharCode = 46) then if (Pos(‘.‘, Edit1.Text) = 0) then Edit1OldProc(Message); if TWMCHAR(Message).CharCode in [48..57, 8, 3] then Edit1OldProc(Message); if (TWMCHAR(Message).CharCode = 22) then Edit1OldProc(Message); end; else Edit1OldProc(Message); end; end; -----------
執行這些代碼, Edit1只能輸入數字,小數點和負號,負號和小數點只能輸入一個,負號必須在最前,粘貼的數字必須完全正確.