Jump to content

inputbox only number double ?


Recommended Posts

i answer me   i create this solution is  not so much pretty style  

Func _control_numb($stringa)

    Local $string_correct = StringReplace($stringa, ',', '.')
    Local $de = StringIsDigit($string_correct)
    If $de = 1 Then
        MsgBox(0, '', 'è un numero')
    Else
        Local $de2 = StringIsFloat($string_correct)
        If $de2 = 1 Then
            MsgBox(0, '', 'è un numero2')
        Else
            MsgBox(0, '', 'non è un numero !!!')
        EndIf
    EndIf
EndFunc   ;==>_control_numb

 

Link to comment
Share on other sites

You can also use something like this :

#Include <EditConstants.au3>
#Include <GuiConstantsEx.au3>
#include <GuiEdit.au3>
#Include <WinAPI.au3>
#Include <WindowsConstants.au3>



GUICreate("gui")

$edit = GUICtrlCreateInput("", 10, 10, 200, 25)


GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd


Func _CheckInput()
    Local Static $sPreviousValue
    Local $sVal = GUICtrlRead($edit), $aSel

    If StringRegExp($sVal, "^\d+\.?\d*$") Then
        $sPreviousValue = $sVal
    Else
        $aSel = _GUICtrlEdit_GetSel ( $edit )
        GUICtrlSetData($edit, $sPreviousValue)
        _GUICtrlEdit_SetSel ( $edit, $aSel[0] - 1, $aSel[1] - 1 )
    EndIf
EndFunc



Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    Local $iIDFrom = _WinAPI_LoWord($wParam)
    Local $iCode = _WinAPI_HiWord($wParam)
    
    Switch $iIDFrom
        Case $edit
            Switch $iCode
                Case $EN_UPDATE
                    _CheckInput()
        EndSwitch
    EndSwitch
            
EndFunc

 

Edited by jguinch
Link to comment
Share on other sites

A little simpler    :)

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>

$gui = GUICreate("test", 300, 200)
$input = GUICtrlCreateInput("", 50, 50, 200, 20, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER))
GUISetState()

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While GUIGetMsg() <> $GUI_EVENT_CLOSE
WEnd


Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Local $IdFrom, $iCode, $read
    $IdFrom = BitAnd($wParam, 0x0000FFFF)
    $iCode = BitShift($wParam, 16)
    Switch $IdFrom
    Case $input
      Switch $iCode
        Case $EN_UPDATE
          $read = GUICtrlRead($input)
          If not StringRegExp($read, "^\d+\.?\d*$") Then GUICtrlSetData($input, StringTrimRight($read, 1))
      EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

 

Link to comment
Share on other sites

14 hours ago, jguinch said:

You can also use something like this :

#Include <EditConstants.au3>
#Include <GuiConstantsEx.au3>
#include <GuiEdit.au3>
#Include <WinAPI.au3>
#Include <WindowsConstants.au3>



GUICreate("gui")

$edit = GUICtrlCreateInput("", 10, 10, 200, 25)


GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd


Func _CheckInput()
    Local Static $sPreviousValue
    Local $sVal = GUICtrlRead($edit), $aSel

    If StringRegExp($sVal, "^\d+\.?\d*$") Then
        $sPreviousValue = $sVal
    Else
        $aSel = _GUICtrlEdit_GetSel ( $edit )
        GUICtrlSetData($edit, $sPreviousValue)
        _GUICtrlEdit_SetSel ( $edit, $aSel[0] - 1, $aSel[1] - 1 )
    EndIf
EndFunc



Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    Local $iIDFrom = _WinAPI_LoWord($wParam)
    Local $iCode = _WinAPI_HiWord($wParam)
    
    Switch $iIDFrom
        Case $edit
            Switch $iCode
                Case $EN_UPDATE
                    _CheckInput()
        EndSwitch
    EndSwitch
            
EndFunc

 

You can't delete the edit content, not the first character, maybe this line:

_GUICtrlEdit_SetSel ( $edit, $aSel[0] - 1, $aSel[1] - 1 )

Work around? Check if Backspace key or DEL key is pressed?

Kind Regards
Alien.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...