dwaynek Posted May 22, 2021 Share Posted May 22, 2021 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 ExitLoop Case $GUI_EVENT_CLOSE Exit Case $idCheckbox If _IsChecked($idCheckbox) Then ;MsgBox($MB_SYSTEMMODAL, "", "The checkbox is checked.", 0, $GUI) Local $bypass=1 Else ;MsgBox($MB_SYSTEMMODAL, "", "The checkbox is not checked.", 0, $GUI) Local $bypass=0 EndIf EndSwitch WEnd Link to comment Share on other sites More sharing options...
Solution Musashi Posted May 22, 2021 Solution Share Posted May 22, 2021 (edited) 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 May 22, 2021 by Musashi typo Netol 1 "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 comment Share on other sites More sharing options...
dwaynek Posted May 23, 2021 Author Share Posted May 23, 2021 cool! thanks! Link to comment Share on other sites More sharing options...
JockoDundee Posted June 1, 2021 Share Posted June 1, 2021 @dwaynek, looks like a clear question with a clear answer. You should vote up @Musashi and mark his post as the solution. Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now