Ir al contenido


Foto

Centralizar Tedit para ingresar Float


  • Por favor identifícate para responder
1 respuesta en este tema

#1 giulichajari

giulichajari

    Advanced Member

  • Miembros
  • PipPipPip
  • 477 mensajes

Escrito 28 septiembre 2017 - 11:24

Queria centralizar el evento OnKeyPress para que todos los edit de dinero o cantidades permitan solo numero, y un solo punto como separador de decimales.

Para lo mismo hice:

 

delphi
  1. procedure TFTicket.EEFECTIVOKeyPress(Sender: TObject; var Key: Char);
  2. begin
  3. Key:=UControl.floatedit(EEFECTIVO,Key);
  4. end;

en la unidad Ucontrol.pas tengo:

 

La funcion toma la tecla presionada y corrobora y devuelve vacio si se presiona una letra o devuelve el numero. Y si no hay punto lo devuelve, de lo contrario vacio-.


delphi
  1. function floatedit(Sender:TEdit;key:Char):Char;
  2. var
  3. keyr:Char;
  4. i:integer;
  5. begin
  6. if not (key in ['0'..'9','.',#8,#27]) then
  7. begin
  8. keyr:=#0;
  9. showmessage('Por favor introduzca numeros');
  10. result:=keyr;
  11. end
  12. else
  13. begin
  14. for i := 1 to length(Sender.Text) do
  15. if ( copy(Sender.Text,i,1) = '.' ) and not ( StrScan('.', Key) = nil ) then Key := #0;
  16.  
  17. result:=key;
  18. end;
  19.  
  20. end;

Saludos

 


  • 1

#2 Agustin Ortu

Agustin Ortu

    Advanced Member

  • Moderadores
  • PipPipPip
  • 831 mensajes
  • LocationArgentina

Escrito 28 septiembre 2017 - 12:37

Yo lo tengo hecho de esta manera, es mas sofisticado pero me permite reemplazar algunos detalles de implementacion en runtime sin tocar la logica de procesamiento:

 


delphi
  1. unit Application.Locale;
  2.  
  3. interface
  4.  
  5. type
  6. {$REGION 'Locale'}
  7. /// <summary> Handles the current system locale settings </summary>
  8. /// <remarks> This is an "ambient-context" (see more at Dependency Injection in .NET by Mark Seemann)
  9. /// The actual work is delegated to a ILocaleSettings interface that can be overriden using the
  10. /// Locale.Current static property. A default implementation is used if the property is not set </remarks>
  11. Locale = record
  12. public type
  13. {$REGION 'ILocaleSettings'}
  14. /// <summary> Handles the current system locale settings </summary>
  15. ILocaleSettings = interface
  16. ['{226AF252-8EB9-47D5-9D3A-EFC8692EDF38}']
  17. function GetCurrencyDecimalSeparator: System.Char;
  18. /// <summary> Returns wether the given Char is valid to be used in the string representation of
  19. /// Integer numbers </summary>
  20. /// <param name="AllowNegative"> Allows a '-' character to be treated as valid </param>
  21. /// <param name="AllowSpace"> Allows a ' ' character to be treated as valid </param>
  22. function IsIntegerChar(
  23. const Char: System.Char;
  24. const AllowNegative: Boolean = True;
  25. const AllowSpace: Boolean = True): Boolean;
  26. /// <summary> Returns True if the given Char is used as the Decimal Separator for either Currency
  27. /// or floating-point numbers (Single, Double, Extended) in their string representation </summary>
  28. function IsDecimalSeparator(const Char: System.Char): Boolean;
  29. /// <summary> Returns wether the given Char is valid to be used in the string representation of
  30. /// Currency or floating-point numbers (Single, Double, Extended) </summary>
  31. function IsCurrencyChar(const Char: System.Char; const AllowNegative: Boolean = True): Boolean;
  32. /// <summary> Returns the Character that is used as decimal separator in the string
  33. /// representation of Currency Values </summary>
  34. property CurrencyDecimalSeparator: System.Char read GetCurrencyDecimalSeparator;
  35. end;
  36. {$ENDREGION}
  37. strict private type
  38. {$REGION 'Locale.TDefaultLocale'}
  39. TDefaultLocale = class(TInterfacedObject, ILocaleSettings)
  40. strict private const
  41. DecimalSeparators = [',', '.'];
  42. Digits = ['0' .. '9'];
  43. Others = [#8];
  44. Negative = '-';
  45. Space = ' ';
  46. AllowedDigitsChars = Digits;
  47. AllowedCurrencyChars = DecimalSeparators + AllowedDigitsChars + Others;
  48. strict private
  49. {$REGION 'ILocaleSettings'}
  50. function IsIntegerChar(const Char: System.Char; const AllowNegative, AllowSpace: Boolean): Boolean;
  51. function IsCurrencyChar(const Char: System.Char; const AllowNegative: Boolean): Boolean;
  52. function GetCurrencyDecimalSeparator: System.Char;
  53. function IsDecimalSeparator(const Char: System.Char): Boolean;
  54. {$ENDREGION}
  55. end;
  56. {$ENDREGION}
  57. strict private
  58. class var FCurrent: Locale.ILocaleSettings;
  59. class function CreateDefault: Locale.ILocaleSettings; static;
  60. class function GetCurrent: Locale.ILocaleSettings; static;
  61. class function GetCurrencyDecimalSeparator: System.Char; static;
  62. class procedure SetCurrent(const Value: Locale.ILocaleSettings); static;
  63. public
  64. /// <summary> Returns wether the given Char is valid to be used in the string representation of
  65. /// Integer numbers </summary>
  66. /// <param name="AllowNegative"> Allows a '-' character to be treated as valid </param>
  67. /// <param name="AllowSpace"> Allows a ' ' character to be treated as valid </param>
  68. class function IsIntegerChar(
  69. const Char: System.Char;
  70. const AllowNegative: Boolean = True;
  71. const AllowSpace: Boolean = True): Boolean; static;
  72. /// <summary> Returns wether the given Char is valid to be used in the string representation of
  73. /// Currency or floating-point numbers (Single, Double, Extended) </summary>
  74. class function IsCurrencyChar(const Char: System.Char; const AllowNegative: Boolean = True): Boolean; static;
  75. /// <summary> Returns True if the given Char is used as the Decimal Separator for either Currency
  76. /// or floating-point numbers (Single, Double, Extended) in their string representation </summary>
  77. class function IsDecimalSeparator(const Char: System.Char): Boolean; static;
  78. /// <summary> Returns the Character that is used as decimal separator in the string representation
  79. /// of Currency Values </summary>
  80. class property CurrencyDecimalSeparator: System.Char read GetCurrencyDecimalSeparator;
  81. /// <summary> The System's Current implementation of the Locale Settings </summary>
  82. class property Current: Locale.ILocaleSettings read GetCurrent write SetCurrent;
  83. end;
  84. {$ENDREGION}
  85.  
  86. implementation
  87.  
  88. uses
  89. System.Guard,
  90. System.SysUtils;
  91.  
  92. {$REGION 'Locale'}
  93.  
  94. class function Locale.CreateDefault: Locale.ILocaleSettings;
  95. begin
  96. Result := Locale.TDefaultLocale.Create;
  97. end;
  98.  
  99. class function Locale.GetCurrencyDecimalSeparator: System.Char;
  100. begin
  101. Result := Locale.Current.CurrencyDecimalSeparator;
  102. end;
  103.  
  104. class function Locale.GetCurrent: Locale.ILocaleSettings;
  105. begin
  106. if not System.Assigned(Locale.FCurrent) then
  107. Locale.FCurrent := Locale.CreateDefault;
  108. Result := Locale.FCurrent;
  109. end;
  110.  
  111. class function Locale.IsIntegerChar(const Char: System.Char; const AllowNegative, AllowSpace: Boolean): Boolean;
  112. begin
  113. Result := Locale.Current.IsIntegerChar(Char, AllowNegative, AllowSpace);
  114. end;
  115.  
  116. class function Locale.IsCurrencyChar(const Char: System.Char; const AllowNegative: Boolean): Boolean;
  117. begin
  118. Result := Locale.Current.IsCurrencyChar(Char, AllowNegative);
  119. end;
  120.  
  121. class function Locale.IsDecimalSeparator(const Char: System.Char): Boolean;
  122. begin
  123. Result := Locale.Current.IsDecimalSeparator(Char);
  124. end;
  125.  
  126. class procedure Locale.SetCurrent(const Value: Locale.ILocaleSettings);
  127. begin
  128. Locale.FCurrent := Guard.CheckNotNull(Value);
  129. end;
  130.  
  131. {$REGION 'Locale.TDefaultLocale'}
  132.  
  133. function Locale.TDefaultLocale.GetCurrencyDecimalSeparator: System.Char;
  134. begin
  135. Result := '.';
  136. end;
  137.  
  138. function Locale.TDefaultLocale.IsIntegerChar(
  139. const Char: System.Char;
  140. const AllowNegative, AllowSpace: Boolean): Boolean;
  141. var
  142. CharSet: TSysCharSet;
  143. begin
  144. CharSet := AllowedDigitsChars;
  145. if AllowNegative then
  146. System.Include(CharSet, Negative);
  147.  
  148. if AllowSpace then
  149. System.Include(CharSet, Space);
  150.  
  151. Result := System.SysUtils.CharInSet(Char, CharSet);
  152. end;
  153.  
  154. function Locale.TDefaultLocale.IsCurrencyChar(const Char: System.Char; const AllowNegative: Boolean): Boolean;
  155. var
  156. CharSet: TSysCharSet;
  157. begin
  158. CharSet := AllowedCurrencyChars;
  159. if AllowNegative then
  160. System.Include(CharSet, Negative);
  161.  
  162. Result := System.SysUtils.CharInSet(Char, CharSet);
  163. end;
  164.  
  165. function Locale.TDefaultLocale.IsDecimalSeparator(const Char: System.Char): Boolean;
  166. begin
  167. Result := System.SysUtils.CharInSet(Char, DecimalSeparators);
  168. end;
  169.  
  170. {$ENDREGION}
  171.  
  172. {$ENDREGION}
  173.  
  174. end.

Ejemplo de uso:


delphi
  1. procedure TForm1.edDescuentoKeyPress(Sender: TObject; var Key: Char);
  2. begin
  3. if not Locale.IsCurrencyChar(Key) then
  4. begin
  5. Key := #0;
  6. Exit;
  7. end;
  8.  
  9. if Locale.IsDecimalSeparator(Key) then
  10. Key := Locale.CurrencyDecimalSeparator;
  11. end;


  • 1




IP.Board spam blocked by CleanTalk.