In my country the decimal separator is ",". One of my clients would like to have it as a "." character. What should I do to change decimal separator to "."?
I've tried this:
procedure TfrmMain.FormCreate(Sender: TObject);
begin
DecimalSeparator := '.';
Application.UpdateFormatSettings := True;
end;
But this code helps only partialy. I see the "." in gird in float fields. However, when user press on numeric keybord a "." key, the comma is send despite the settings. This is problem not related to the grid, I've checked this on KeyPress event on the form.
I am using Delphi 2007, thanks for your help.
First of all, you should set UpdateFormatSettings to false! If this property is true, the DecimalSeparator will be reset to the Windows default one every now and then (for instance, every time you lock the workstation (using Win+L) and then unlock it again). The default value is true, so you need to set it to false when you want to override DecimalSeparator.
Secondly, the DecimalSeparator is used by Delphi routines when formatting floating-point numbers as strings (e.g. when using FloatToStr or FormatFloat). To make the decimal separator key on the numeric keypad result in a point (.) and not the OS default character (which is probably either . or ,), you can handle the OnKeyPress event:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if Key = ',' then
Key := '.'
end;
But be cautious - this will replace all , with ., even those inserted by the comma key on the alphabetical part of the keyboard.
A more advanced (and safer) method is to handle the OnKeyDown event like this:
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
msg: TMsg;
begin
if Key = VK_DECIMAL then
begin
PeekMessage(msg, Edit1.Handle, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE);
SendMessage(Edit1.Handle, WM_CHAR, ord('.'), 0);
end;
end;
What char this key maps to isn't determined by Delphi, but by the keyboard layout set in windows.
One hack would be simply replacing , by . in the OnKeyPress handler.
Else you need to somehow modify the Translation of the KeyDown/Up messages to the key press messages, but I don't know how to do that.
The Translation from virtual keys to chars happens in TranslateMessage http://msdn.microsoft.com/en-us/library/ms644955(VS.85).aspx
So you could check the message passed in to TranslateMessage beforehand, check if it is the virtual decimal key on the numpad and then pass in a different virtual key. But everything you can do is pretty hackish.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With