Sign in to follow this  
Followers 0
ryantollefson

Input style with ES_NUMBER

10 posts in this topic

I have an input control that I want to limit to numerical values, so I specified $ES_NUMBER as the style for the control.

The problem is that this won't let the user put in a negative number - it says that the "-" is an "Unacceptable Character." Is there any way to allow only this character and numbers?

Thanks in advance.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

get this UDF by MsCreator

_GUIInputSetOnlyNumbers() UDF, Set input control to accept only numbers

Edit: edit regexp in GuiInputSetOnlyNumbers.au3 to your needs

I changed it to accommodate floating numbers

to return numeric values instead of a string use Number() function when reading the input control

$iNum = Number(GUICtrlRead($Input))

modify the UDF and change reg expression to allow negative numbers and float

Func _Input_Changed($hWnd, $CtrlId)
    ToolTip("")
    Local $Read_Input = GUICtrlRead($CtrlId)
    If StringRegExp($Read_Input, '[^0-9]') Then
        GUICtrlSetData($CtrlId, StringRegExpReplace($Read_Input, '[^0-9]', ''))

;TO

    If StringRegExp($Read_Input, '[^-0-9.]') Then
        GUICtrlSetData($CtrlId, StringRegExpReplace($Read_Input, '[^-0-9.]', ''))
Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites

Hi! Try this:

#include <GuiConstants.au3>

$hGui = GUICreate("Test GUI", 300, 200)

$hInput = GUICtrlCreateInput("", 50, 50, 200, 20)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

GUISetState()

While GUIGetMsg() <> -3
WEnd

Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Local $IdFrom, $iCode, $iGetInput, $iRegExp
    
    $IdFrom = BitAnd($wParam, 0x0000FFFF) ;loword
    $iCode = BitShift($wParam, 16) ;hiword
    
    Switch $IdFrom
    Case $hInput
        Switch $iCode
            Case $EN_UPDATE
                $iGetInput = GUICtrlRead($hInput)
                $iRegExp = StringRegExpReplace($iGetInput, "(\d|-)", "")
                If $iRegExp <> "" Then
                    $iGetInput = StringTrimRight($iGetInput, 1)
                    GUICtrlSetData($hInput, $iGetInput)
                EndIf
        EndSwitch
    EndSwitch
    
    Return $GUI_RUNDEFMSG
EndFunc
:)

Share this post


Link to post
Share on other sites

@rasim

Nice concise version

rover


I see fascists...

Share this post


Link to post
Share on other sites

Thank you both, both examples work :)

I think I'm probably going to use the UDF just because I have a number of input fields that I want to apply this to, and I think the UDF will be easier to implement.

Just one more question though, both methods will allow a user to enter something like "654-321" - clearly this isn't a normal number... is there a way that I could check to make sure the "-" is only allowed as a first character?

Share this post


Link to post
Share on other sites

is there a way that I could check to make sure the "-" is only allowed as a first character?

Yes, i thought that there will be an easy way using RegExp pattern, but i can't figuring it out... replace this function in the UDF file:

Func _Input_Changed($hWnd, $CtrlId)
    ToolTip("")
    
    Local $Read_Input = GUICtrlRead($CtrlId)
    Local $iShowWarning = True
    
    If StringRegExp($Read_Input, '[^0-9]') Then
        If StringLeft($Read_Input, 1) = "-" Then
            $Read_Input = "-" & StringRegExpReplace($Read_Input, '[^0-9]', '')
            $iShowWarning = False
        Else
            $Read_Input = StringRegExpReplace($Read_Input, '[^0-9]', '')
        EndIf
        
        GUICtrlSetData($CtrlId, $Read_Input)
        
        Local $Gui_Get_Pos = WinGetPos($hWnd)
        Local $Ctrl_Get_Pos = ControlGetPos($hWnd, "", $CtrlId)
        
        Local Const $SM_CYCAPTION = 4 ;Titelbar heigth
        Local Const $SM_CXFIXEDFRAME = 7 ;Window border size
        
        If $iShowWarning Then
            Local $X_Pos = $Gui_Get_Pos[0] + $Ctrl_Get_Pos[0] + $Ctrl_Get_Pos[2] + GetSystemMetrics($SM_CXFIXEDFRAME)
            Local $Y_Pos = $Gui_Get_Pos[1] + $Ctrl_Get_Pos[1] + $Ctrl_Get_Pos[3] + GetSystemMetrics($SM_CYCAPTION)
            
            ToolTip($sToolTip_Text[0], $X_Pos, $Y_Pos, $sToolTip_Text[1], 3, 1+4)
            DllCall("user32.dll", "int", "MessageBeep", "int", 0x0)
        EndIf
    EndIf
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

allows only single "-" minus sign and only at start of string

a second decimal point in number displays in input field but not returned nor are digits after it when guictrlread

entering illegal characters backspaces one character too many, dont have fix for that.

If StringRegExp($Read_Input, '[^\A-{1}][^0-9.]') Then
    GUICtrlSetData($CtrlId, StringRegExpReplace($Read_Input, '[^\A-{1}][^0-9.]', ''))

RegEx not fun......

Edit: really not fun, now entering illegal characters as first character is displayed...

close, but not there

Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites

Thanks MsCreatoR that seems to work well for what I'm doing. :)

Good luck getting that rover - I just want whole numbers, not decimals; so mine is happy now.

Share this post


Link to post
Share on other sites

Yes, i thought that there will be an easy way using RegExp pattern

definitely no easy way: see regexp code below

I used w0uter's StringRegExp design Gui

to play around with regexp, something I need to get a better handle on.

this was as good an excuse as any to put in some effort to learn.

Anyway, for anyone who's interested

this mod to MsCreators UDF allows negative numbers with the input controls

Func _Input_Changed($hWnd, $CtrlId)
    ToolTip("")
    Local $Read_Input = GUICtrlRead($CtrlId)
    If StringRegExp($Read_Input, '[^\d-]|[{0-9,1}^\A-][^\d]') Then
        $Read_Input = StringRegExpReplace($Read_Input, '[^\d-]|([{0-9,1}^\A-])[^\d]', '\1')
        GUICtrlSetData($CtrlId, $Read_Input)
        Local $Gui_Get_Pos = WinGetPos($hWnd)
        Local $Ctrl_Get_Pos = ControlGetPos($hWnd, "", $CtrlId)
        Local Const $SM_CYCAPTION = 4       ; Titlebar height
        Local Const $SM_CXFIXEDFRAME = 7    ; Window border size
        Local $X_Pos = $Gui_Get_Pos[0] + $Ctrl_Get_Pos[0] + $Ctrl_Get_Pos[2] + GetSystemMetrics($SM_CXFIXEDFRAME)
        Local $Y_Pos = $Gui_Get_Pos[1] + $Ctrl_Get_Pos[1] + $Ctrl_Get_Pos[3] + GetSystemMetrics($SM_CYCAPTION)
        ToolTip($sToolTip_Text[0], $X_Pos, $Y_Pos, $sToolTip_Text[1], 3, 1+4)
        DllCall("user32.dll", "int", "MessageBeep", "int", 0x0)
    EndIf
EndFunc

I see fascists...

Share this post


Link to post
Share on other sites

I knew there will be a RegExp solution for that one :) - Thanks @rover!

RegExp is the King of the String! :)


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
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
Sign in to follow this  
Followers 0