I found a code here that I needed. To only allow write numbers in a text box. But I still wanted more, which does not offer up the \"Next\" button without write the number i
There are two flaws in the @TLama's answer:
A user can bypass the check by using Paste command from edit box context menu (and possibly by other kinds of input methods).
To fix this, you can introduce a last resort check in NextButtonClick.
I would also suggest adding the check for an empty input there, instead of MyEditChange, as it allows providing feedback to the user, explaining what is wrong.
On the other hand, Ctrl+C, Ctrl+V and Ctrl+X keys are not working. Particularly a lack of Ctrl+V is not comfortable.
To fix this, allow these control characters in the MyEditKeyPress.
[Code]
var
MyEdit: TNewEdit;
MyPage: TWizardPage;
procedure ValidateMyEdit;
begin
{ enable the next button if the edit box is not empty; disable otherwise }
WizardForm.NextButton.Enabled := (MyEdit.Text <> '');
end;
procedure MyEditChange(Sender: TObject);
begin
ValidateMyEdit;
end;
function IsDigit(C: Char): Boolean;
begin
Result := (C >= '0') and (C <= '9')
end;
procedure MyEditKeyPress(Sender: TObject; var Key: Char);
begin
if not ((Key = #8) or { Tab key }
(Key = #3) or (Key = #22) or (Key = #24) or { Ctrl+C, Ctrl+V, Ctrl+X }
IsDigit(Key)) then
begin
Key := #0;
end;
end;
procedure InitializeWizard;
begin
MyPage := CreateCustomPage(wpWelcome, 'Caption', 'Description');
MyEdit := TNewEdit.Create(WizardForm);
MyEdit.Parent := MyPage.Surface;
MyEdit.Left := 0;
MyEdit.Top := 0;
MyEdit.Width := 150;
MyEdit.OnChange := @MyEditChange;
MyEdit.OnKeyPress := @MyEditKeyPress;
end;
procedure CurPageChanged(CurPageID: Integer);
begin
if CurPageID = MyPage.ID then
begin
ValidateMyEdit;
end;
end;
function NextButtonClick(CurPageID: Integer): Boolean;
var
I: Integer;
begin
Result := True;
if CurPageID = MyPage.ID then
begin
for I := 1 to Length(MyEdit.Text) do
begin
if not IsDigit(MyEdit.Text[I]) then
begin
MsgBox('Only numbers are allowed', mbError, MB_OK);
Result := False;
Break;
end;
end;
end;
end;