Jump to content

How Limit the Keys in an Input Control


 Share

Recommended Posts

I would like to know if I can limit the key aceppted in an GUICtrlInput to only this keys [+,.0123456789]

I tryed with GUIRegisterMsg($WM_CHAR, "TestChar") but it doesn't work the function TestChar is never called.

I tryed also with control style $ES_NUMBER but it only accept [0123456789] keys

here is the code

#include <GUIConstants.au3>
Global Const $WM_CHAR = 0x0102
;Global Const $WM_KEYDOWN = 0x0100
; == GUI generated with Koda ==
if not GUIRegisterMsg($WM_KEYDOWN, "TestChar") then Exit
$Form1 = GUICreate("AForm1", 366, 173, 192, 125)
$Input1 = GUICtrlCreateInput("", 40, 40, 241, 21, $ES_NUMBER, $WS_EX_CLIENTEDGE)
$Button1 = GUICtrlCreateButton("Ok", 216, 96, 97, 33)
GUICtrlCreateLabel("Escriba algo por favor", 40, 16, 107, 17)
GUISetState(@SW_SHOW)
$EditVal = ""
While 1
    $msg = GuiGetMsg()
    $NewVal = GUICtrlRead($Input1)
    Select
        Case $EditVal <> $NewVal
            ConsoleWrite("New Edit Value is = " & $NewVal & @CRLF)
            $EditVal = $NewVal
    Case $msg = $GUI_EVENT_CLOSE or $msg = $Button1
        ExitLoop
    Case Else
        ;;;;;;;
    EndSelect
WEnd
Exit

Func TestChar($hWnd, $Msg, $wParam, $lParam)
    MsgBox(64,"Char", "Char = " & Chr($lParam))
    ConsoleWrite("Char Pressed = " & Chr($wParam))
    Return $GUI_RUNDEFMSG
EndFunc

Thanks in advance

Link to comment
Share on other sites

There likely is a shorter way, but

While 1
    GUIGetMsg()
    Select
        Case
    EndSelect
    If Not IsNumber StringRight(GUICtrlRead($input), 1) And StringRight(GUICtrlRead($input), 1) <> "." And StringRight(GUICtrlRead($input), 1) <> "+" And StringRight(GUICtrlRead($input), 1) <> "," Then
        GUICtrlSetData($input, StringTrimRight(GUICtrlRead($input), 1))
    EndIf
WEnd

Alzo

Don't take my pic to serious...~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~You Looked, but you did not see!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Link to comment
Share on other sites

There likely is a shorter way, but

While 1
    GUIGetMsg()
    Select
        Case
    EndSelect
    If Not IsNumber StringRight(GUICtrlRead($input), 1) And StringRight(GUICtrlRead($input), 1) <> "." And StringRight(GUICtrlRead($input), 1) <> "+" And StringRight(GUICtrlRead($input), 1) <> "," Then
        GUICtrlSetData($input, StringTrimRight(GUICtrlRead($input), 1))
    EndIf
WEnd

Alzo

Thanks marfdaman, I know about this alternative, but I'm looking if there is another way to capture the keystroke before the key is placed in the Input Control via windows message, maybe.
Link to comment
Share on other sites

I faced similar problem - needed to do price entry with KeyPad Dot adding .00 to price.

I ended up using a hotkey. Here is example for Keypad period and Plus.

You will need Hotkeys for all non numeric keys.

Also, make sure you unregister Hotkeys when not in your GUI.

HotKeySet("{NUMPADDOT}", "Dot")  
HotKeySet("{NUMPADADD}", "Plus")
#region --- GuiBuilder code Start ---
; Script generated by AutoBuilder 0.6 Prototype

#include <GuiConstants.au3>

GuiCreate("Numeric only input", 214, 156,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))

$Input_1 = GuiCtrlCreateInput("", 20, 50, 90, 20,BitOR($ES_RIGHT,$ES_NUMBER))

GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case Else
    ;;;
    EndSelect
WEnd
Exit
#endregion --- GuiBuilder generated code End ---

Func Dot()
    GUICtrlSetData($Input_1,GUICtrlRead($Input_1) & ".")
EndFunc

Func Plus()
    GUICtrlSetData($Input_1,"+" & GUICtrlRead($Input_1))
EndFunc
Link to comment
Share on other sites

GuiCtrlSetLimit??!!

or did i misunderstand the question

Yes you did (I did as well when I first read it), he means to limit the different characters that can be typed, not the number of chars.

Don't take my pic to serious...~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~You Looked, but you did not see!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Link to comment
Share on other sites

Yes you did (I did as well when I first read it), he means to limit the different characters that can be typed, not the number of chars.

many thanks again marfdaman, both answer are usefull but I wish to implement a more complet input mask, and I'm looking for other way to Solve it, now I'll try with OnEvent Mode if I have luck I will show you.

Thanks again all of you

Sorry about my english

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...