jacQues Posted January 1, 2011 Share Posted January 1, 2011 In the example below I use $GUI_DEFBUTTON once. As expected, the first time an Enter won't press any button and the second time it will press button 2. But the third time it continues to press button 2. What am I missing/doing wrong?? expandcollapse popup#include <GUIConstantsEx.au3> global $aButton[4] = [0] local $iMsg GUICreate("test") GUICtrlCreateLabel("Test...",10,10) GUISetState() MakeButtons(110) Wait4Click() DeleteButtons() MakeButtons(210) GUICtrlSetState($aButton[2],$GUI_DEFBUTTON) Wait4Click() DeleteButtons() MakeButtons(310) Wait4Click() func Wait4Click() while true $iMsg = GUIGetMsg() if $iMsg>0 then for $i = 1 to $aButton[0] if $iMsg=$aButton[$i] then MsgBox(0,"test","Button "&String($i)&"...") return endif next ;endif elseif $iMsg=$GUI_EVENT_CLOSE then MsgBox(0,"test","Close...") return endif wend endfunc func DeleteButtons() for $i = 1 to $aButton[0] GUICtrlDelete($aButton[$i]) next $aButton[0] = 0 endfunc func MakeButtons($iX) $aButton[0] = 3 $aButton[1] = GUICtrlCreateButton("button 1",$iX,110) $aButton[2] = GUICtrlCreateButton("button 2",$iX,210) $aButton[3] = GUICtrlCreateButton("button 3",$iX,310) endfunc Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 1, 2011 Moderators Share Posted January 1, 2011 jacQues,Add a line to your MakeButtons function so you can see the returned ControlIDs: Func MakeButtons($iX) $aButton[0] = 3 $aButton[1] = GUICtrlCreateButton("button 1", $iX, 110) $aButton[2] = GUICtrlCreateButton("button 2", $iX, 210) $aButton[3] = GUICtrlCreateButton("button 3", $iX, 310) ConsoleWrite("1: " & $aButton[1] & @CRLF & "2: " & $aButton[2] & @CRLF & "3: " & $aButton[3] & @CRLF & @CRLF) EndFunc ;==>MakeButtonsYou can see that AutoIt reuses the same ControlIDs each time you create the buttons. This is because AutoIt ControlIDs are actually the indices of an internal array where AutoIt manages its controls. Each time you create a control AutoIt takes the lowest available element of that array and passes back the index as the ControlID. But if you delete a control, AutoIt frees the element and it will get reused by the next created control - remember, Autoit looks for the lowest available space, not the next highest.So the buttons have the same ControlIDs each time you create them and the GUICtrlSetState($aButton[2], $GUI_DEFBUTTON) still applies to any control with that ControlID.Al clear? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Zedna Posted January 1, 2011 Share Posted January 1, 2011 Of course I know all the Melba said but anyway it seems like interesting problem and maybe bug in Autoit. Even after GUICtrlDelete it probably internally remember its previous state ($GUI_DEFBUTTON) and reuse it as default after next GUICtrlCreate. I tried to make workaround with dummy control but it doesn't work: expandcollapse popup#include <GUIConstantsEx.au3> global $aButton[4] = [0] local $iMsg GUICreate("test") GUICtrlCreateLabel("Test...",10,10) $dummy = GUICtrlCreateDummy() GUISetState() MakeButtons(110) Wait4Click() DeleteButtons() MakeButtons(210) GUICtrlSetState($aButton[2],$GUI_DEFBUTTON) Wait4Click() DeleteButtons() MakeButtons(310) Wait4Click() func Wait4Click() while true $iMsg = GUIGetMsg() if $iMsg>0 then for $i = 1 to $aButton[0] if $iMsg=$aButton[$i] then MsgBox(0,"test","Button "&String($i)&"...") return endif next ;endif elseif $iMsg=$GUI_EVENT_CLOSE then MsgBox(0,"test","Close...") return endif wend endfunc func DeleteButtons() for $i = 1 to $aButton[0] GUICtrlDelete($aButton[$i]) next $aButton[0] = 0 GUICtrlSetState($dummy,$GUI_FOCUS) GUICtrlSetState($dummy,$GUI_DEFBUTTON) endfunc func MakeButtons($iX) $aButton[0] = 3 $aButton[1] = GUICtrlCreateButton("button 1",$iX,110) $aButton[2] = GUICtrlCreateButton("button 2",$iX,210) $aButton[3] = GUICtrlCreateButton("button 3",$iX,310) endfunc Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
jacQues Posted January 1, 2011 Author Share Posted January 1, 2011 Thanks so much for the very quick replies! So now I know why this is happening. But how can I 'reset' this behaviour neatly? For now, I made the following 'dirty' workaround: ; Use after building the GUI and before looping for user interaction: GUICtrlCreateButton("",0,0) GUICtrlSetState(-1,$GUI_DEFBUTTON) GUICtrlDelete(-1) This does seem like a bug to me, but Melba23 seems to suggest that it is expected operation. I guess the thing I'm missing is either GUICtrlSetState($control,$GUI_NODEFBUTTON) or even GUISetState(@SW_NODEFBUTTON), since there can only be one control with DEFBUTTON... jacQues Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 1, 2011 Moderators Share Posted January 1, 2011 (edited) jacQues,there can only be one control with DEFBUTTONFrom my searching and coding this afternoon I have found that $GUI_DEFBUTTON is is a rather strange beast. Although it is set by GUICtrlSetState, it appears to be linked more to the GUI rather than the control itself. I can also find no way of "unsetting" the value - it seems that you can only "reset" it to another button, so your work-a-round seems the only way to go. I will keep looking. M23Edit: I have found a few archived Dev Chat topics where DEFBUTTON is discussed. The general opinion from those who know seems to be that Windows does not handle default buttons well and that even with AutoIt's custom code to handle such things there are likely to be occasions where the default does not default to what you think it should - if you see what I mean! Edited January 2, 2011 by Melba23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area 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