Recent

Author Topic: [UNSOLVED] Replace the decimal point with a comma in a DBEDIT  (Read 2303 times)

Ever

  • Jr. Member
  • **
  • Posts: 77
Hello everyone

      I need to know if there is any way so that when I am writing a numerical value in a DBEDIT (this DBEDIT is linked to a numerical field of a MySQL table), and I need to add the decimal part, when I press the point on the keyboard, it gives me a eat. I don't want to have to resort to changing Windows settings for this purpose. I want to do it by programming.

    First of all, Thanks
« Last Edit: May 03, 2024, 01:16:38 am by Ever »

Thaddy

  • Hero Member
  • *****
  • Posts: 16556
  • Kallstadt seems a good place to evict Trump to.
Re: Replace the decimal point with a comma in a DBEDIT
« Reply #1 on: April 27, 2024, 06:25:14 am »
Add a field OldFormatSettings:TFormatSettings; to your form.
Then use the OnEnter and Onleave events from the TDbEdit like so:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Enter(Sender: TObject);
  2. begin
  3.    OldFormatSettings:=FormatSettings;
  4.    FormatSettings.DecimalSeparator:='.';
  5. end;
  6.  
  7. procedure TForm1.Edit1Exit(Sender: TObject);
  8. begin
  9.   FormatSettings := OldFormatSettings;
  10. end;
This temporary changes the DecimalSeparator to a dot, but restores it to the system default after leaving the TDbEdit.
The rest of your controls in your application will behave according  to the system default settings.
Tip: combine it with the CustomEditMask and EditMask properties.

« Last Edit: April 27, 2024, 06:46:33 am by Thaddy »
But I am sure they don't want the Trumps back...

Ever

  • Jr. Member
  • **
  • Posts: 77
Re: Replace the decimal point with a comma in a DBEDIT
« Reply #2 on: April 27, 2024, 03:02:14 pm »
Thanks for your reply

      Unfortunately that doesn't work, what I want is to be able to press the key corresponding to the dot on the keyboard and have a comma typed instead.

    I have tried using the DBEDIT OnKeyPress event to replace the dot with the comma in the following way

Code: Pascal  [Select][+][-]
  1.        if Key=#46 then Key:=#44;
  2.  

   but it does not work. If I carry that same action in a TEdit component it takes it perfectly.

cdbc

  • Hero Member
  • *****
  • Posts: 1868
    • http://www.cdbc.dk
Re: Replace the decimal point with a comma in a DBEDIT
« Reply #3 on: April 27, 2024, 03:18:07 pm »
Hi
Hmmm...
You could try 'OnKeyDown' instead of keypress and then something like this:
Code: Pascal  [Select][+][-]
  1. uses LCLTypes; // i think this is the one with the VK_ keycodes
  2. ...
  3. procedure TForm1.DBEdit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  4. begin
  5.   case Key of                    
  6.     VK_Punctuation: Key:= VK_Comma; // not sure about the names
  7.   end;
  8. end;
  9.  
Untested code, but worth a try, I think.... %)
Regards Benny
« Last Edit: April 27, 2024, 03:19:57 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Ever

  • Jr. Member
  • **
  • Posts: 77
Re: Replace the decimal point with a comma in a DBEDIT
« Reply #4 on: April 27, 2024, 04:05:22 pm »
Hello Benny

      It didn't work either. I make the note that the constants are no longer used, check the LCLType code, VK_Comma is commented and VK_Punctuation does not appear, I still replaced the constants with the respective values in ASCII code and it didn't work. I think the problem is due to the fact that it is a dbedit and is linked to a database field. If it's not possible, I'll unfortunately leave it that way.

    Thanks for your support

wp

  • Hero Member
  • *****
  • Posts: 12615
Re: Replace the decimal point with a comma in a DBEDIT
« Reply #5 on: April 27, 2024, 05:54:57 pm »
In which country are you? What is the decimalseparator on your system? What does the following mini project print on the screen, a comma or a point?
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses
  3.   SysUtils;
  4. begin
  5.   WriteLn(Formatsettings.DecimalSeparator);
  6.   ReadLn;
  7. end.
If it is a comma then your system is set up to want floating point values with a decimal comma. But then why do you want to enter a decimal point? According to https://dev.mysql.com/doc/refman/8.0/en/fixed-point-types.html, MySQL (your database system?) stores floating point values in a binary format in which it does not matter whether there is a comma or point decimal separator. Only the conversion between text and float is important. This is done by the TDBEdit which is well-prepared for this issue and is able to convert the entered string (with a comma or point, depending on your FormatSettings) into a valid binary representation.

RayoGlauco

  • Full Member
  • ***
  • Posts: 194
  • Beers: 1567
Re: Replace the decimal point with a comma in a DBEDIT
« Reply #6 on: April 27, 2024, 06:47:47 pm »
Note that the virtual key codes are not ASCII codes.

See this: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

You can use VK_OEM_COMMA and VK_OEM_PERIOD in KeyDown events
To err is human, but to really mess things up, you need a computer.

Ever

  • Jr. Member
  • **
  • Posts: 77
Re: Replace the decimal point with a comma in a DBEDIT
« Reply #7 on: April 27, 2024, 11:03:30 pm »
I completely understand wp's point of view and your analysis is completely accurate. In Venezuela all PCs are configured with the comma for the decimal part and it happens that out of habit, 100% of the users got used to typing the decimal point when they use Excel, and it places the comma automatically, and it is difficult for them to understand that In the case of software developed with Pascal, not to mention other languages, they must type the comma and not the point in the decimal part, most people assume that for any software it is how Excel does it.

For this reason I wanted to try to ensure that the behavior that is natural for people, such as in Excel, is the same in the software I develop.

RayoGlauco

  • Full Member
  • ***
  • Posts: 194
  • Beers: 1567
Re: Replace the decimal point with a comma in a DBEDIT
« Reply #8 on: April 27, 2024, 11:17:23 pm »
After putting the correct names, @cdbc's code would look like this:
Code: Pascal  [Select][+][-]
  1. uses LCLType; //  VK_ keycodes
  2. ...
  3. procedure TForm1.DBEdit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  4. begin
  5.   case Key of                    
  6.     VK_OEM_PERIOD : Key:= VK_OEM_COMMA;
  7.   end;
  8. end;
To err is human, but to really mess things up, you need a computer.

wp

  • Hero Member
  • *****
  • Posts: 12615
Re: Replace the decimal point with a comma in a DBEDIT
« Reply #9 on: April 28, 2024, 12:24:04 am »
In Venezuela all PCs are configured with the comma for the decimal part and it happens that out of habit, 100% of the users got used to typing the decimal point when they use Excel, and it places the comma automatically
In German, this does not work. We also have the comma as decimal separator, but the point as date separator. So, when I type "1.2" it gets converted to "01.Feb"... There would also be a problem with the thousand separator which is a point here. So, if Excel were allowed to convert an entered decimal point to a comma there would be an ambiguity: When I type "1.200" would this mean "one-point-two" or "one-thousand-two-hundred"?

In my opinion, attempts to make numeric input more flexible regarding the decimal separator are risky. When you are sure that all users type a decimal point although the country settings of the OS are set up for a decimal comma you should not rely on individual software to take care, but really change the country settings of the operating system to a decimal separator. Then everything would be consistent again for all applications.

Ever

  • Jr. Member
  • **
  • Posts: 77
Re: Replace the decimal point with a comma in a DBEDIT
« Reply #10 on: April 28, 2024, 12:30:29 am »
In the case of the thousands separator, it is the point, so it did not open ambiguity. But given all the options, I have decided to let it work as it does, and let the user adapt.

Thank you all for your support

jamie

  • Hero Member
  • *****
  • Posts: 6801
Re: Replace the decimal point with a comma in a DBEDIT
« Reply #11 on: April 28, 2024, 12:57:09 pm »
You can't use the "OnChange" event in the DBEdit and filter the first "." with a replacement of a "," ?
The only true wisdom is knowing you know nothing

Ever

  • Jr. Member
  • **
  • Posts: 77
Replace the decimal point with a comma in a DBEDIT
« Reply #12 on: May 03, 2024, 01:15:39 am »
Thank you all for your support, I made the decision to leave it as it works in dbedit

jcmontherock

  • Sr. Member
  • ****
  • Posts: 278
Re: [UNSOLVED] Replace the decimal point with a comma in a DBEDIT
« Reply #13 on: May 03, 2024, 11:10:03 am »
I'm using:
Code: Pascal  [Select][+][-]
  1. function EditNum(lValue: Double): String;
  2. var
  3.   fs: TFormatSettings;
  4. begin
  5.   result := '0';
  6.   if lValue = 0  then  exit;
  7.   {$WARNINGS OFF}{$HINTS OFF}
  8.   GetLocaleFormatSettings(GetSystemDefaultLCID, fs);
  9.   {$WARNINGS ON}{$HINTS ON}
  10.   fs.ThousandSeparator := '''';
  11.   fs.DecimalSeparator  := '.';
  12.   result := FormatFloat('###,###,###.##', lValue, fs); // ,: thousand separator, .: decimal separator
  13. end;
  14.  
Windows 11 UTF8-64 - Lazarus 4.0RC2-64 - FPC 3.2.2

 

TinyPortal © 2005-2018