get numeric with %

Post Reply
User avatar
Silvio.Falconi
Posts: 7253
Joined: Thu Oct 18, 2012 7:17 pm
Has thanked: 2 times
Been thanked: 29 times

get numeric with %

Post by Silvio.Falconi »

Image

I have a problem in the numeric tget class

I have to insert 5 discounts example 12.90% I wish have 12,9 %

the fields on archive is 6 digit and 2 decimal

the problem is that the customer wants to insert the data quickly and finds it difficult

if he inserts 12345 automatically the get puts it correctly i.e. 123.45%

if instead he has to insert 10.9% he cannot insert it i.e. if he presses the point or comma after 10
the get goes to the next get

If I press "." or "," the get goes to the next get

I tried to check the digits with aGet[1]:bChange := {|nKey|(Chr(nKey)$"0123456789.")}

or chech the key with aGet[1]:bKeyDown := { | nKey |MyKeyHandler( nKey, @nSconto1 ) }

I tried with picture "@EZ 999,99 %" and "@E ###,## %"


this is the test

Code: Select all | Expand



#include "fivewin.ch"
#include 'constant.ch'

#define DLG_nColorDlg     RGB(245,245,235)


FUNCTION Main()
   local oDlg
   local nRow:= 10,nCol:=10
   local nBottom:= 9
   local nRight := 75
   local nWd := Max( nRight * DLG_CHARPIX_W, 180 )
   local nHt := nBottom * DLG_CHARPIX_H
   local oFont,oBold
   local oSay:=array(1)
   local aGet:=array(5)
   local cPicture:= "@EZ 999,99 %" //"@E ###,## %"
   local nSizeGetSconto := 60
   local nSconto1 := 0,;
         nSconto2 := 0,;
         nSconto3 := 0,;
         nSconto4 := 0,;
         nSconto5 := 0

   oFont := TFont():New( "Arial", 0, -11 )
   oBold := TFont():New( "Arial", 0, -11,,.t. )

   DEFINE DIALOG oDlg SIZE nWd, nHt PIXEL TRUEPIXEL;
       FONT oFont  TITLE "test get numerico %" COLOR CLR_BLACK, DLG_nColorDlg  ;
       STYLE nOR( DS_MODALFRAME, WS_POPUP, WS_CAPTION, WS_SYSMENU, ;
                  WS_MINIMIZEBOX)

   @ nRow, nCol SAY oSay[1] PROMPT "Sconto %:" OF oDlg SIZE 75, 18 PIXEL FONT oFont  TRANSPARENT
   nCol+=110

   @ nRow, nCol GET aGet[1] VAR nSconto1 SIZE nSizeGetSconto, 18 PIXEL RIGHT;
      PICTURE  cPicture;
      OF oDlg

            aGet[1]:bChange  := {|nKey|(Chr(nKey)$"0123456789.")}
          //aGet[1]:bKeyDown := { | nKey |MyKeyHandler( nKey, @nSconto1 ) }

    nCol+=78
    @ nRow, nCol GET aGet[2] VAR nSconto2 SIZE nSizeGetSconto, 18 PIXEL RIGHT;
      PICTURE  cPicture;
      OF oDlg
    nCol+=78
    @ nRow, nCol GET aGet[3] VAR nSconto3 SIZE nSizeGetSconto, 18 PIXEL RIGHT;
      PICTURE  cPicture;
      OF oDlg
    nCol+=78
    @ nRow, nCol GET aGet[4] VAR nSconto4 SIZE nSizeGetSconto, 18 PIXEL RIGHT;
      PICTURE  cPicture;
      OF oDlg
    nCol+=78
    @ nRow, nCol GET aGet[5] VAR nSconto5 SIZE nSizeGetSconto, 18 PIXEL RIGHT;
      PICTURE  cPicture;
      OF oDlg

   // Bottone di conferma
   @ 90, 100 BUTTON "OK" SIZE 80, 30 PIXEL ACTION oDlg:End()

   ACTIVATE DIALOG oDlg CENTERED

   RETURN NIL



 FUNCTION MyKeyHandler( nKey, nVar )
   LOCAL cKey := Chr(nKey)
   LOCAL cVal
   IF cKey == "," .OR. cKey == "."
      cVal := LTrim(Str(nVar, 10, 2))
      IF "." $ cVal .OR. "," $ cVal
         RETURN 0
      ENDIF
      cVal += ","
      nVar := Val( StrTran(cVal, ",", ".") )
      RETURN 0
   ENDIF

RETURN nKey



any solution pls ?
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Otto
Posts: 6426
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 48 times
Been thanked: 14 times
Contact:

Re: get numeric with %

Post by Otto »

Silvio, can't you switch to a character GET and convert on exit?
Best regards,
Otto
User avatar
Silvio.Falconi
Posts: 7253
Joined: Thu Oct 18, 2012 7:17 pm
Has thanked: 2 times
Been thanked: 29 times

Re: get numeric with %

Post by Silvio.Falconi »

Otto wrote: Sat Apr 19, 2025 10:19 pm Silvio, can't you switch to a character GET and convert on exit?
Best regards,
Otto
Big problem
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
Posts: 7253
Joined: Thu Oct 18, 2012 7:17 pm
Has thanked: 2 times
Been thanked: 29 times

Re: get numeric with %

Post by Silvio.Falconi »

Otto wrote: Sat Apr 19, 2025 10:19 pm Silvio, can't you switch to a character GET and convert on exit?
Best regards,
Otto
Or modify KeyChar method
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Otto
Posts: 6426
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 48 times
Been thanked: 14 times
Contact:

Re: get numeric with %

Post by Otto »

Silvio, First, "Happy Easter"
Best regards,
Otto

ChatGPT – suggests this to me – I haven’t tested it – but the idea isn’t bad:

Instead of using a numeric GET with PICTURE, use a character GET, let the user type freely, and convert the value when leaving the field. This way, you avoid having `,` or `.` act as tab commands. Example:

Code: Select all | Expand

@ 10,10 GET oGet VAR cBuf SIZE 60,18 OF oDlg
oGet:bChange := {|nKey| Chr(nKey) $ "0123456789,." .OR. nKey == VK_BACK ? nKey : 0 }
oGet:Valid  := { ||
   c := StrTran( cBuf, ",", "." )        // comma → period
   n := Val( c )                         // convert to numeric
   IF n < 0 .OR. n > 100
      MsgStop( "Invalid value: " + c )
      RETURN .F.
   ENDIF
   nSconto1 := n                              // assign to your numeric variable
   RETURN .T.
}
– this way you keep full control over input and formatting.
User avatar
Silvio.Falconi
Posts: 7253
Joined: Thu Oct 18, 2012 7:17 pm
Has thanked: 2 times
Been thanked: 29 times

Re: get numeric with %

Post by Silvio.Falconi »

perhaps

aGet[1]:bChange := {|nKey|(Chr(nKey)$"0123456789.,")}

aGet[1]:bValid := { ||
cBuf:= aGet[1]:ctext
c := StrTran( cBuf, ",", "." ) // comma ? period
n := Val( c ) // convert to numeric
IF n < 0 .OR. n > 100
MsgStop( "Invalid value: " + c )
RETURN .F.
ENDIF
nSconto1 := n // assign to your numeric variable
RETURN .T.
}

but not run make the same


see the tget class line 2335

Code: Select all | Expand

if ::oGet:Type == "N" .and. ;
              ( Chr( nKey ) == "." .or. Chr( nKey ) == "," )
              if ::oGet:Clear()
              #ifndef __XHARBOUR__
                 ::oGet:DelEnd()
              #endif
              endif
              ::oGet:ToDecPos()



seem not process the todecpos() function
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
Posts: 7253
Joined: Thu Oct 18, 2012 7:17 pm
Has thanked: 2 times
Been thanked: 29 times

Re: get numeric with %

Post by Silvio.Falconi »

this run

I tested with "," and with "."

Image

at init there is only a get

Code: Select all | Expand

#include "fivewin.ch"
#include 'constant.ch'

#define DLG_nColorDlg RGB(245,245,235)

FUNCTION Main()
   local oDlg, oFont, oBold
   local nRow := 10, nCol := 10
   local nBottom := 9
   local nRight := 75
   local nWd := Max( nRight * DLG_CHARPIX_W, 180 )
   local nHt := nBottom * DLG_CHARPIX_H
   local oSay := array(5)
   local aGet := array(5)
   local cPicture := "@E 999999.99 %" // Formato per 6 cifre intere e 2 decimali
   local nSizeGetSconto := 60
   local nSconto1 := 0, nSconto2 := 0, nSconto3 := 0, nSconto4 := 0, nSconto5 := 0

   oFont := TFont():New( "Arial", 0, -11 )
   oBold := TFont():New( "Arial", 0, -11,, .t. )

   DEFINE DIALOG oDlg SIZE nWd, nHt PIXEL TRUEPIXEL;
      FONT oFont TITLE "Test Get Numerico %" COLOR CLR_BLACK, DLG_nColorDlg;
      STYLE nOR( DS_MODALFRAME, WS_POPUP, WS_CAPTION, WS_SYSMENU, WS_MINIMIZEBOX)

   @ nRow, nCol SAY oSay[1] PROMPT "Sconto %:" OF oDlg SIZE 75, 18 PIXEL FONT oFont TRANSPARENT
   nCol += 110

   @ nRow, nCol GET aGet[1] VAR nSconto1 SIZE nSizeGetSconto, 18 PIXEL RIGHT;
      PICTURE cPicture OF oDlg;
      VALID {|| (MyValid( aGet[1] ),UpdateSconti(nSconto1, nSconto2, nSconto3, nSconto4, nSconto5, aGet, oSay, oDlg)) }
   aGet[1]:bKeyDown := { |nKey| MyKeyHandler( nKey, aGet[1] ) }

   nCol += 78
     @ nRow, nCol SAY oSay[2] PROMPT "+" OF oDlg SIZE 10, 20 PIXEL FONT oFont TRANSPARENT
   nCol+=15
   @ nRow, nCol GET aGet[2] VAR nSconto2 SIZE nSizeGetSconto, 18 PIXEL RIGHT;
      PICTURE cPicture OF oDlg;
      VALID {|| (MyValid( aGet[2] ),UpdateSconti(nSconto1, nSconto2, nSconto3, nSconto4, nSconto5, aGet, oSay, oDlg)) }
   aGet[2]:bKeyDown := { |nKey| MyKeyHandler( nKey, aGet[2] ) }

   nCol += 78
     @ nRow, nCol SAY oSay[3] PROMPT "+" OF oDlg SIZE 10, 20 PIXEL FONT oFont TRANSPARENT
   nCol+=15
   @ nRow, nCol GET aGet[3] VAR nSconto3 SIZE nSizeGetSconto, 18 PIXEL RIGHT;
      PICTURE cPicture OF oDlg;
           VALID {|| (MyValid( aGet[3] ),UpdateSconti(nSconto1, nSconto2, nSconto3, nSconto4, nSconto5, aGet, oSay, oDlg)) }
   aGet[3]:bKeyDown := { |nKey| MyKeyHandler( nKey, aGet[3] ) }

   nCol += 78
     @ nRow, nCol SAY oSay[4] PROMPT "+" OF oDlg SIZE 10, 20 PIXEL FONT oFont TRANSPARENT
   nCol+=15
   @ nRow, nCol GET aGet[4] VAR nSconto4 SIZE nSizeGetSconto, 18 PIXEL RIGHT;
      PICTURE cPicture OF oDlg;
            VALID {|| (MyValid( aGet[4] ),UpdateSconti(nSconto1, nSconto2, nSconto3, nSconto4, nSconto5, aGet, oSay, oDlg)) }
   aGet[4]:bKeyDown := { |nKey| MyKeyHandler( nKey, aGet[4] ) }

   nCol += 78
     @ nRow, nCol SAY oSay[5] PROMPT "+" OF oDlg SIZE 10, 20 PIXEL FONT oFont TRANSPARENT
   nCol+=15
   @ nRow, nCol GET aGet[5] VAR nSconto5 SIZE nSizeGetSconto, 18 PIXEL RIGHT;
      PICTURE cPicture OF oDlg;
           VALID {|| (MyValid( aGet[5] ),UpdateSconti(nSconto1, nSconto2, nSconto3, nSconto4, nSconto5, aGet, oSay, oDlg)) }
   aGet[5]:bKeyDown := { |nKey| MyKeyHandler( nKey, aGet[5] ) }

   @ 90, 100 BUTTON "OK" SIZE 80, 30 PIXEL ACTION oDlg:End()

   ACTIVATE DIALOG oDlg CENTERED ;
   ON INIT UpdateSconti(nSconto1, nSconto2, nSconto3, nSconto4, nSconto5, aGet, oSay, oDlg)

RETURN NIL

FUNCTION MyKeyHandler( nKey, oGet )
   if oGet:oGet:Type == "N" .and. ( Chr( nKey ) == "." .or. Chr( nKey ) == "," )
      oGet:oGet:Assign( StrTran( oGet:oGet:Buffer, ".", "," ) )
      oGet:oGet:ToDecPos()
      oGet:Refresh()
      return 0
   endif
RETURN nKey

FUNCTION MyValid( oGet )
   local nValue := oGet:Value
   if nValue < 0 .or. nValue > 999999.99
      MsgAlert( "Valore non valido! Deve essere tra 0 e 999999,99", "Errore" )
      return .F.
   endif
   RETURN .T.


   static Function UpdateSconti(nSconto1, nSconto2, nSconto3, nSconto4, nSconto5, aGet, oSay, oDlgEdit)
  // Sconto 2
   if nSconto1 > 0
      oSay[2]:Show()
      aGet[2]:Show()
   else
      oSay[2]:Hide()
      aGet[2]:Hide()
      nSconto2 := 0
   endif

   // Sconto 3
   if nSconto2 > 0
      oSay[3]:Show()
      aGet[3]:Show()
   else
      oSay[3]:Hide()
      aGet[3]:Hide()
      nSconto3 := 0
   endif

   // Sconto 4
   if nSconto3 > 0
      oSay[4]:Show()
      aGet[4]:Show()
   else
      oSay[4]:Hide()
      aGet[4]:Hide()
      nSconto4 := 0
   endif

   // Sconto 5
   if nSconto4 > 0
      oSay[5]:Show()
      aGet[5]:Show()
   else
      oSay[5]:Hide()
      aGet[5]:Hide()
      nSconto5 := 0
   endif


return .T.

/code]


but at init I see "0,00%"   not Know how   change to "0%"


[b]look carefully [/b] this function , so I call todecpos() becuse keychar method of tget class ( line 2335) not  is processed

[code]FUNCTION MyKeyHandler( nKey, oGet )
   if oGet:oGet:Type == "N" .and. ( Chr( nKey ) == "." .or. Chr( nKey ) == "," )
      oGet:oGet:Assign( StrTran( oGet:oGet:Buffer, ".", "," ) )
      oGet:oGet:ToDecPos()
      oGet:Refresh()
      return 0
   endif
RETURN nKey
the problem remains that at the beginning I see in the get 0.00% instead of 0%
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Post Reply