Jump to content

how to set default focus on a button when creating a gui

Go to solution Solved by Musashi,

Recommended Posts

when creating a gui, how do i set a button as the default focus (eg the OK button) so that when the gui is shown, the default focus is on the ok button and when i press ENTER it activates it as if it was clicked on.

this is my sample code. when the gui is shown, the focus seems to be on the ok button but when i press ENTER nothing happens which isn't the expected result.

$GUI = GUICreate("chkonejav", 500, 200)
    Local $Input1 = GUICtrlCreateEdit("Enter url", 10, 10, 480, 150, $ES_MULTILINE)
    GUICtrlSetData($Input1, $clip)
    $OKbut = GUICtrlCreateButton("OK", 10, 170, 50, 25)
    ; Create a checkbox control.
    Local $idCheckbox = GUICtrlCreateCheckbox("Bypass?", 70, 170, 185, 25)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $GUI)

    _WinAPI_SetFocus(ControlGetHandle("chkonejav", "", $OKbut)) ; sets default focus on ok button

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $OKbut
            Case $GUI_EVENT_CLOSE
            Case $idCheckbox
                If _IsChecked($idCheckbox) Then
                    ;MsgBox($MB_SYSTEMMODAL, "", "The checkbox is checked.", 0, $GUI)
                    Local $bypass=1
                    ;MsgBox($MB_SYSTEMMODAL, "", "The checkbox is not checked.", 0, $GUI)
                    Local $bypass=0


Link to post
Share on other sites
  • Solution

One way :

#include <ButtonConstants.au3>
$OKbut = GUICtrlCreateButton("OK", 10, 170, 50, 25, $BS_DEFPUSHBUTTON)

or :

$OKbut = GUICtrlCreateButton("OK", 10, 170, 50, 25)
GUICtrlSetState($OKbut, $GUI_FOCUS)

EDIT : You don't need the line _WinAPI_SetFocus... anymore

Edited by Musashi


"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to post
Share on other sites
  • 2 weeks later...

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...