How to allow entering only alphanumeric chars in a TInputQueryWizardPage edit box?

StackOverflow https://stackoverflow.com/questions/23215745

  •  07-07-2023
  •  | 
  •  

Pregunta

I need to allow entering only alphanumeric chars in an Inno Setup's TInputQueryWizardPage edit box. How can I do this ?

¿Fue útil?

Solución

To recognize an alphanumeric char I would use the IsCharAlphaNumeric Windows API function and in the assigned input edit's OnKeyPress event I would eat the key if it won't be alphanumeric:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif

function IsCharAlphaNumeric(ch: Char): BOOL;
  external 'IsCharAlphaNumeric{#AW}@user32.dll stdcall';

procedure InputQueryEditKeyPress(Sender: TObject; var Key: Char);
begin
  // if the currently pressed key is not alphanumeric, eat it by
  // assigning #0 value
  if not IsCharAlphaNumeric(Key) then
    Key := #0;
end;

procedure InitializeWizard;
var
  EditIndex: Integer;
  InputPage: TInputQueryWizardPage;
begin
  // create input query page and add one edit item
  InputPage := CreateInputQueryPage(wpWelcome, 'Caption', 'Description',
    'SubCaption');
  EditIndex := InputPage.Add('Name:', False);
  // assign the OnKeyPress event method to our custom one
  InputPage.Edits[EditIndex].OnKeyPress := @InputQueryEditKeyPress;
end;

Otros consejos

This is also another method of code for allowing only alphanumeric keys, which also allows you to use the Backspace key:

procedure InputQueryEditKeyPress(Sender: TObject; var Key: Char);
var
  KeyCode: Integer;
begin
  KeyCode := Ord(Key);
  if not ((KeyCode = 8) or ((KeyCode >= 48) and (KeyCode <= 57)) or ((KeyCode >= 65) and (KeyCode <= 90)) or ((KeyCode >= 97) and (KeyCode <= 122))) then
    Key := #0;
end;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top