Input of floating point numeric number in a window

Just starting out? Need help? Post your questions and find answers here.
Rudy M
User
User
Posts: 46
Joined: Fri Aug 23, 2024 1:18 pm

Input of floating point numeric number in a window

Post by Rudy M »

Hello everyone,

In my programs, I'm currently trying to work only with the PB window routines (no sequences with an open console...).

I see the editor gadget for entering text.
But what do you do to enter a floating-point number (Float...) in a window?

Is there anything possible with the Editor gadget, or has anyone found another solution?

Best regards,
Rudy M
Sergey
User
User
Posts: 57
Joined: Wed Jan 12, 2022 2:41 pm

Re: Input of floating point numeric number in a window

Post by Sergey »

Code: Select all

input.s = InputRequester("Enter a float number", "Float:", "")
If input
	float.f = ValF(input)	
	Debug "Float is " + StrF(float)
Else
	Debug "Canceled"
EndIf
Without checking of introduced characters
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: Input of floating point numeric number in a window

Post by Axolotl »

If you are on windows and not afraid of using API maybe this can be a start

Code: Select all

EnableExplicit 
Global g_bNumericOnly = #False 

CompilerIf Not (#PB_Compiler_OS = #PB_OS_Windows)  
  MessageRequester("Sorry ....", "This works for windows, only.", #PB_MessageRequester_Error)  
  End 
CompilerEndIf 

Procedure EditorSubClassProc(hWnd, uMsg, wParam, lParam) 
  Static s_prevFunc = 0 
  
  Select uMsg  
    Case -1   ; <== Not recommended, but working -- So we call it for advanced users only :) 
      s_prevFunc = SetWindowLongPtr_(hWnd, #GWL_WNDPROC, @EditorSubClassProc()) 

    Case #WM_CHAR 
      If g_bNumericOnly 
        If (wParam >= #VK_0 And wParam <= #VK_9) Or wParam = ',' 
          ; forward to default procedure 
        ElseIf wParam = '.' 
          ; okay, because we changed the char and forward to default procedure 
          wParam = ',' 
        Else 
          ; all other chars are not allowed, no call of the default procedure 
          ProcedureReturn 0 
        EndIf 
      EndIf 
  EndSelect 
  ProcedureReturn CallWindowProc_(s_prevFunc, hWnd, uMsg, wParam, lParam)  
EndProcedure 


Procedure Main() 
  Protected temp$ 

  If OpenWindow(0, 0, 0, 322, 190, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

    EditorGadget(0, 8, 8, 306, 133)
    CheckBoxGadget(1, 8, 168, 120, 24, "Numeric Only") 

    EditorSubClassProc(GadgetID(0), -1, 0, 0) 

    Repeat 
      Select WaitWindowEvent() 
        Case #PB_Event_CloseWindow 
          Break 

        Case #WM_CHAR 
          Debug "MainLoop: CHAR  " + EventwParam()  ; you cannot change the value of the virtual key here. 

        Case #PB_Event_Gadget 
          Select EventGadget() 
            Case 1 
              g_bNumericOnly = GetGadgetState(1)        :Debug "g_bNumericOnly = " + g_bNumericOnly 
              SetActiveGadget(0) 
          EndSelect
      EndSelect 
    ForEver 
  EndIf
EndProcedure

End Main() 

BTW: There is a modern way of doing subclass, but you need to import some api functions, and that makes this example a little to heavy.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: Input of floating point numeric number in a window

Post by RASHAD »

Hi
Without posting a workable snippet can't guess your requirement
But you can use Str,StrD,StrF

Code: Select all

  If OpenWindow(0, 0, 0, 322, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    EditorGadget(0, 8, 8, 306, 133)
    For a = 0 To 5
      AddGadgetItem(0, a, "Line "+Str(a)+" " +StrF(14678.2345))
    Next
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
Egypt my love
User avatar
spikey
Enthusiast
Enthusiast
Posts: 767
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Input of floating point numeric number in a window

Post by spikey »

You might want to have a look at Numeric Entry into a EditBox.
Rudy M
User
User
Posts: 46
Joined: Fri Aug 23, 2024 1:18 pm

Re: Input of floating point numeric number in a window

Post by Rudy M »

Thank You all for the programming-hints and for the link.
Now I have some work to do to test it out :D

Rudy M
Sergey
User
User
Posts: 57
Joined: Wed Jan 12, 2022 2:41 pm

Re: Input of floating point numeric number in a window

Post by Sergey »

Axolotl wrote: Mon Aug 25, 2025 12:42 pm If you are on windows and not afraid of using API maybe this can be a start
Also need a check, only one ',' char may be used :wink:
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: Input of floating point numeric number in a window

Post by Axolotl »

Sergey wrote: Thu Aug 28, 2025 1:18 pm Also need a check, only one ',' char may be used :wink:
Yeah, you are right. But since I don't know exactly what he has in mind, it's just a start. Help for self-help, so to speak.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
RNBW
User
User
Posts: 72
Joined: Thu Jan 02, 2014 5:01 pm

Re: Input of floating point numeric number in a window

Post by RNBW »

The first thing to remember is that data entered into an Editbox is text only. When you enter a number it is simply the characters that you are entering. To use the entry numerically, it must be restricted to numerical characters 0 to 9 and - and . (or , if you use , as the decimal point). You must also restrict the number of minus signs and decimal point to 1. To use the number entered you must convert it from text to a number.

If you Search for Numeric Entry in the Forum, you will find several versions that can be used.

The link in spikey's post is one of these. It is for use in the UK where a . is used as the decimal point. It should be easy enough to convert it to a , if that is what you use.

I hope this is useful for you and you will be able to develop your program.
Post Reply