Sign in to follow this  
Followers 0
joseLB

how to give focus to a button?

7 posts in this topic

Hi all

I need to make a button to receive focus, after some user input in other input fields.

I tried GUICtrlSetState($btScan,$GUI_FOCUS), or ControlFocus($titA0FasEntry,"","Button1") and nothing makes the button to receive focus so I could type <enter> to execute it.

The point is that I'm doing a small numeric dataentry system, with 2 numeric fields and a button, and the desirable sequence is:

  • user types the numbers in an input field in numpad keyboard and press <enter>,
  • the next input field be the selected one, and user again types numbers and <enter>
  • a button is selected so that if user types <enter>, the button does it activities.
  • the 1st input field becomes the focused one again and all the sequence repeats.
For the input fields, GUICtrlSetState($scanFim,$GUI_FOCUS) works fine, they receive focus and <enter> work fine, but for the button it does nothing...

Thanks

Jose

Share this post


Link to post
Share on other sites



There is probably a far better way but.... what if you set "enter" as a hot key to run a function that contains an "if" statement. I would think you could use "ControlGetFocus" to find out what step you are at and take the appropriate action, set focus to control or run function.


Share this post


Link to post
Share on other sites

Thanks JMeyer, that's a good solution!

But I'm guessing why the button does not receive focus as the input fileds receive. That can be considered a bug? :P

Share this post


Link to post
Share on other sites

You can set the state of a button to "$BS_DEFPUSHBUTTON", this will cause the same action as pressing the button would but by pressing enter. But I would think just setting the the enter key to run a function would better serve your situation.


Share this post


Link to post
Share on other sites

you'r right, "...just setting the the enter key to run a function would better serve your situation... as $BS_DEFPUSHBUTTON seems to me would do that button the permanent default to <enter> key...

Thanks again,

Jose

Share this post


Link to post
Share on other sites

Very Basic:

#include <GUIConstants.au3>
HotKeySet("{ENTER}", "SelectControl")
Global $controled = 1
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("AForm1", 135, 136, 193, 115)
$Input1 = GUICtrlCreateInput("AInput1", 8, 8, 121, 21)
$Input2 = GUICtrlCreateInput("AInput2", 8, 40, 121, 21)
$Input3 = GUICtrlCreateInput("AInput3", 8, 72, 121, 21)
$Button1 = GUICtrlCreateButton("AButton1", 56, 104, 75, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func SelectControl()
    If WinActive($Form1) Then
        If $controled = 1 Then
            GUICtrlSetState($Input2, $GUI_FOCUS)
            $controled = 2
        ElseIf $controled = 2 Then
            GUICtrlSetState($Input3, $GUI_FOCUS)
            $controled = 3
        ElseIf $controled = 3 Then
            GUICtrlSetState($Button1, $GUI_FOCUS)
            $controled = 4
        ElseIf $controled = 4 Then
            GUICtrlSetState($Input1, $GUI_FOCUS)
            $controled = 1
            _InsertData()
        EndIf
    EndIf
EndFunc  ;==>SelectControl

Func _InsertData()
    $1 = GUICtrlRead($Input1)
    $2 = GUICtrlRead($Input2)
    $3 = GUICtrlRead($Input3)
    MsgBox(0, "Results", "1 = " & $1 & @CRLF & "2 = " & $2 & @CRLF & "3 = " & $3)
EndFunc  ;==>_InsertData

Other things you could add are the likes of getting the current control focus and such.

Hope it gives you a start :P

Share this post


Link to post
Share on other sites

Thanks Brett and jMeyer. I will go with hotkeyset..Jose

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