Ultimate Delphi Resource from DelphiBasics. Delphi tips and tricks. Project Tutorials. Delphi and Pascal source code samples. > an edit box that will only except numbers. I think there are a few around. Now, that looks promising. A message we can trap with a message handler, so add a handler for WM_PASTE to the control. The declaration. The TMaskEdit component allows the numeric mask but does not have an alignment property, nor does the Editbox component. How do I get the right-justification? Alas, neither the TMaskEdit nor the TEdit components have a property 'Alignment'.
I'd use a TButtonedEdit to get the button, and to enforce floating-point input with a maximum of two decimals after the point, I'd do TButtonedEdit = class(ExtCtrls.TButtonedEdit) protected procedure KeyPress(var Key: Char); override; procedure WMPaste(var Message: TWMPaste); message WMPASTE; end. Procedure TButtonedEdit.KeyPress(var Key: Char); function InvalidInput: boolean; var dc: integer; begin result:= false; if Character.IsControl(Key) then Exit; dc:= Pos(DecimalSeparator, Text); if not (Key in '0'.' 9', DecimalSeparator) then Exit(true); if Pos(DecimalSeparator, Text) 0 then begin if Key = DecimalSeparator then Exit(true); if (Length(Text) - dc 1) and (Pos(DecimalSeparator, Text).
I am using the following code for a simple edit box that accepts only numeric input. The problem is that the edit box's OnChange notification event doesn't fire. I'm not sure what needs to be added to get the default behaviour back for valid keypresses. Procedure TNumericEdit.Keypress(Sender: TObject; var Key: Char); begin if not (Key in #8, '0'.' ) then begin Key:= #0; exit; end; if ((Key = '.' ) or (Key = '-')) and (Pos(Key, (Sender as TEdit).Text) 0) then begin Key:= #0; exit; end; if (Key = '-') and ((Sender as TEdit).SelStart 0) then begin Key:= #0; exit; end; end.
Quote I am using the following code for a simple edit box that accepts only numeric input. The following works for me: procedure AllowOnlyRealInput(Temp: string; var Key: char); var S: Single; Temp2: string; Code: Integer; begin if not (Key=#8) then begin if not (Key in '0'.' ,'-') then Key:=#0; if ((Pos('-',Temp)=0) and (Length(Temp)=5)) or (Length(Temp)=6) then Key:=#0; if (Key='.' ) and (Pos('.' ,Temp)0) then Key:=#0; if (Length(Temp)0) and (Key='-') then Key:=#0; Temp2:=Temp+Key; Val(Temp2,S,Code); if (Abs(S)=MaxInput) then Key:=#0 end end; procedure TMainForm.EditKeyPress(Sender: TObject; var Key: Char); var TempStr: string; begin TempStr:=(Sender as TEdit).Text; AllowOnlyRealInput(TempStr,Key) end; All of the relevant edit boxes have their OnKeyPress event set to TMainForm.EditKeyPress. All of the edits also have their OnChange event set to a procedure that recalculates results any time the text in an edit is changed so, I know the OnChange is firing.