Jump to content

Help with keeping cursor in an input field after enabling a button


crazyjts
 Share

Recommended Posts

Hi all,

I have the following snippet of code that sort of works but isn’t doing exactly what I want.

expand popup
#include <GuiConstants.au3>

Global $gGUIWidth = 250
Global $gGUIHeight = 150

Initial()

Func Initial()

   ; Create the initial GUI
   $BaseGUI = GUICreate("", $gGUIWidth, $gGUIHeight, -1, -1, -1, $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW)

   GUISetState(@SW_SHOW, $BaseGUI)

   ; Create a field for entering the current value
   GUICtrlCreateLabel("Current Value:",  10, 10)
   $CurrentValueFld = GUICtrlCreateInput("", 10, 40, 215, 20, $ES_NUMBER)
   GUICtrlSetState($CurrentValueFld, $GUI_FOCUS)

   ; Create Calculate button
   Local $CalculateButton = GUICtrlCreateButton("Calculate", 10, 70, 75, 25)
   GUICtrlSetState($CalculateButton, $GUI_DISABLE)

   ; Watch the value field and determine if the value is greater than 0 and if so, enable Calculate button
   $i = 0
   Do
      $CurrentValue = GUICtrlRead($CurrentValueFld)
      If $CurrentValue > 0 Then
         GUICtrlSetState($CalculateButton, $GUI_ENABLE + $GUI_FOCUS)
         $i = 1
      EndIf
   until $i = 1

   ;Loop until the user exits and check for ESC, "X", or Begin button
   While 1
      $InitialGUIMsg = GUIGetMsg()
      If $InitialGUIMsg = $GUI_EVENT_CLOSE Then
         Exit
      EndIf
   WEnd
EndFunc

 

I want to be able to:

  1.   Start with focus in the value field (it is doing this now)
  2.  Allow for more than one digit to be entered into the field (it is sort of doing this now but I have to bring focus back to the field)
  3. Enable the Calculate button and give focus to it once a value is entered into the value field (it is doing this now) but keep the cursor in the value field so the more digits can be entered (it is not doing this now)
  4. Be able to hit ESC or “X” to close the dialog at any time (this only works after I enter a digit in the value field

Basically, I want to be able to allow the user to enter one or more digits and hit ENTER or SPACE (while the cursor is still in the value field) to push the Calculate button.  I assume I need to bring focus to the button but maybe I don't and can get away with assigning hotkeys for ENTER and SPACE to push the Calculate button while keeping focus in the value field.

If you could, please add comments in your code as to what the changes are doing if they aren't obvious to a noobish programmer.  

Thanks for any help you can offer on this.

Link to comment
Share on other sites

No need to continue to press the calc button, you could just update the value every time the input is altered.

#include <GuiConstants.au3>
#include <WindowsConstants.au3>

Global $gGUIWidth = 250
Global $gGUIHeight = 150
; Good idea to declare your variables used for the GUI has global, so there's access to them from other functions
Global $BaseGUI = GUICreate("", $gGUIWidth, $gGUIHeight, -1, -1, -1, $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW)

; Create a field for entering the current value
Global $CurrentValueFld = GUICtrlCreateInput("", 10, 40, 215, 20, $ES_NUMBER)

; Create Calculate button
Local $CalculateButton = GUICtrlCreateButton("Calculate", 10, 70, 75, 25)
Global $lblValue = GUICtrlCreateLabel("Current Value:", 10, 10, 230)

; Sett he states of your buttons
GUICtrlSetState($CalculateButton, $GUI_DISABLE)
GUICtrlSetState($CurrentValueFld, $GUI_FOCUS)
GUISetState(@SW_SHOW, $BaseGUI)

; Register the WM_COMMAND for this GUI, certain codes from the Windows OS will be sent to the GUI when it is being used, WM_COMMAND is one of these codes
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While (True)
    ; Use Local, Dim, or Global to declare your variables, good practice!
    Local $InitialGUIMsg = GUIGetMsg()
    Switch ($InitialGUIMsg)
        Case $GUI_EVENT_CLOSE
            GUIDelete($BaseGUI)
            Exit 0
        Case $CalculateButton
            GUICtrlSetData($lblValue, "Current Value: " & GUICtrlRead($CurrentValueFld))
    EndSwitch
WEnd

; The WM_COMMAND Function, these will capture certain commands sent from the Windows OS!
Func WM_COMMAND($hWndFrom, $iMsg, $wParam, $lParam)
    #forceref $hWndFrom, $wParam
    ; The $wParam holds the ID of the control that has focus and code that control is sending
    Local $iIDFrom = BitAND($wParam, 0xFFFF) ; Low Word
    Local $iCode = BitShift($wParam, 16) ; Hi Word

    ; Switch based on the GUI that has focus, good practice to use this even if you only have one GUI
    Switch ($hWndFrom)
        Case $BaseGUI   ; Your $BaseGUI has focus
            ; Switch based on the control ID
            Switch ($iIDFrom)
                Case $CurrentValueFld ; The input box is sending a code
                    ; Switch based on the code your input box is sending
                    Switch ($iCode)
                        Case $EN_CHANGE ; The input box is sending the changing code, the user is adding, or removing, stuff from the input box
                            ; Update your label with the current input value
                            GUICtrlSetData($lblValue, "Current Value: " & GUICtrlRead($CurrentValueFld))
                            ; Set the state of your button (now obsolete)
                            GUICtrlSetState($CalculateButton, $GUI_ENABLE)
                    EndSwitch
            EndSwitch
    EndSwitch

    ; Return the default message, there were no errors
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Since your input only accepts numbers you won't be able to do any math (unless you do it on your own) but if you want to add the ability to calculate an expression use

Execute(Expression)

Ex. Execute($input_data)
Local $input_date = "32+4+10/2"

 

Edited by InunoTaishou
Link to comment
Share on other sites

try this

#include <GuiConstants.au3>

Global $gGUIWidth = 250
Global $gGUIHeight = 150
Global $BaseGUI = GUICreate("", $gGUIWidth, $gGUIHeight, -1, -1, -1, $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW)

; Create a field for entering the current value
GUICtrlCreateLabel("Current Value:", 10, 10)
Global $CurrentValueFld = GUICtrlCreateInput("", 10, 40, 215, 20, $ES_NUMBER)
GUICtrlSetState($CurrentValueFld, $GUI_FOCUS)

; Create Calculate button
Local $CalculateButton = GUICtrlCreateButton("Calculate", 10, 70, 75, 25)
;~ GUICtrlSetState($CalculateButton, $GUI_DISABLE)

; Hot keys Set
Global $AcceptESC = GUICtrlCreateDummy() ;press ESC to exit
Global $AcceptEnter = GUICtrlCreateDummy() ;press ENTER to Calculate
Dim $AccelKeys[2][2] = [["{ENTER}", $AcceptEnter], ["{ESC}", $AcceptESC]]
GUISetAccelerators($AccelKeys)

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            GUIDelete($BaseGUI)
            Exit 0
        Case $AcceptESC
            GUIDelete($BaseGUI)
            Exit 0
        Case $CalculateButton
            If GUICtrlRead($CurrentValueFld) > 1 Then
                MsgBox(262144, "", "input reads " & GUICtrlRead($CurrentValueFld))
            EndIf
        Case $AcceptEnter
            If GUICtrlRead($CurrentValueFld) > 1 Then
                MsgBox(262144, "", "input reads " & GUICtrlRead($CurrentValueFld))
            EndIf
    EndSwitch
    Sleep(10)
WEnd

 

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

×
×
  • Create New...