Search results for 'Comma Separator'. 1 post(s) found.

  1. 2007/08/25 How To Force The Correct Decimal Separator
2007/08/25 09:39

How To Force The Correct Decimal Separator


Some English speaking countries use the "point" as Decimal Separator, while the rest of the world is using the "Comma". How can you make sure that the user will press the correct key when entering a number with a decimal fraction? With the code in this examples, it doesn't matter!

// Correct the Decimal Separator

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  // Delphi automatically sets DecimalSeparator (a global
  // variable) to Windows' Decimal Separator
  if Key in ['.', ','] then
    Key :=  DecimalSeparator;
end;

// This is a more complete procedure:
// it allows only valid numbers and
// it corrects the Decimal Separator.
// For educational purposes 8-) 
// the code is very detailed.

procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
type
  TErrNum = (eNone, eInvalidKey, eTooManySeps, eTooManySigns, eSignPos);
const
  ErrMessages : array[eInvalidKey..eSignPos] of string = (
    'Invalid key',
    'Only one Decimal Separator is allowed',
    'Only one sign is allowed',
    'A sign is only allowed in the first position');
  Signs = ['+', '-'];
  Nums = ['0'..'9'];
  DecSeps = ['.', ','];
  Backspace = [#8];
  ValidKeys = Signs + Nums + DecSeps + Backspace;
var
  ErrNum: TErrNum;
begin
  ErrNum := eNone;
  if not (Key in ValidKeys) then
    ErrNum := eInvalidKey
  else if Key in DecSeps then begin
    Key := DecimalSeparator;
    if Pos(Key, Edit2.Text) > 0 then
      ErrNum := eTooManySeps;
  end
  else if Key in Signs then begin
    if Pos('+', Edit2.Text) + Pos('-', Edit2.Text) > 0 then
      ErrNum := eTooManySigns
    else if Edit2.SelStart <> 0 then
      ErrNum := eSignPos;
  end;
  if ErrNum <> eNone then begin
    Key := #0; // nothing is passed to the Edit
    MessageDlg(ErrMessages[ErrNum], mtWarning, [mbOk], 0);
  end;
end;

Trackback 6 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject Get propecia online pharmacy.

    Tracked from Propecia side efects. 2008/12/14 05:20 delete

    Propecia. Propecia merck propecia online uk. Propecia side effects finasteride. Propecia sexual side effects. Buy propecia international pharmacy. Danger propecia.

  2. Subject Phentermine.

    Tracked from Phentermine information. 2008/12/18 01:02 delete

    Phentermine. Order phentermine.

  3. Subject Soma.

    Tracked from Soma beds. 2008/12/18 04:00 delete

    Soma addiction and lying. Drug soma.

  4. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 00:50 delete

    moneyideas

  5. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 09:16 delete

    moneyideas

  6. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:45 delete

    moneyideas