J'imagine que cela doit être facile, mais comment empêcher un utilisateur de renter dans un TEdit autre chose que les lettres de l'alphabet? Juste a-z et A-Z?
J'imagine que cela doit être facile, mais comment empêcher un utilisateur de renter dans un TEdit autre chose que les lettres de l'alphabet? Juste a-z et A-Z?
Bonjour
question classic, Réponse classic. Utiliser le onKeyPress pour tester le charactère. si vous ne voulez pas l'autoriser, il sufit de mettre 0 (en ascii) dedans.
Attention aux code spéciaux (Enter Up Dow etc..)
Exemple pour un floatprocedure TFMaster.dxFloatKeyPress(Sender: TObject; var Key: Char);
var
AText: string;
begin
if (ord(key) < VK_HELP) and ( not (key in [' ', '.', ','])) then exit;
if Sender is TdxBarEdit then
AText := ( Sender as TdxBarEdit).Text
else if Sender is TEdit then
AText := ( Sender as TEdit).TExt
else
exit;
// . and , are decimal separator
if (Key = ',') or (key = '.') then
key := DecimalSeparator;
// if key is not numeric and key is not the decimal separator the key is not good
if ((key < '0') or (Key > '9')) and (Key <> DecimalSeparator) then
Key := #00;
// if key = decimal separator and if the decimalseparator is in the text,
// Key is a bad value (only one decimal separator in a float)
if key = DecimalSeparator then begin
if pos(DecimalSeparator, AText) > 0 then
Key := #00;
end;
end;
et attention aussi aux Ctrl+V et autres qui peuvent coller n'importe quoi et ne sont pê pas filtrables au OnKeyPress ??? (dans ce cas, coller un popupmenu vide à l'Edit)
Delphi 5 Pro - Delphi 11.3 Alexandria Community Edition - CodeTyphon 6.90 sous Windows 10 ; CT 6.40 sous Ubuntu 18.04 (VM)
. Ignorer la FAQ Delphi et les Cours et Tutoriels Delphi nuit gravement à notre code !
Partager