Jump to content

$GUI_DEFBUTTON seens to stick after GUICtrlDelete()


jacQues
 Share

Recommended Posts

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??

#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

  • Moderators

jacQues,

Add a line to your MakeButtons function so you can see the returned ControlIDs: :P

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   ;==>MakeButtons

You 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? :x

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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:

#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
Link to comment
Share on other sites

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

  • Moderators

jacQues,

there can only be one control with DEFBUTTON

From 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. :shifty:

I will keep looking. :P

M23

Edit: 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! :x:nuke:

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...