Jump to content

GUICtrlSetBkColor problem


Recommended Posts

Just spent 2 hrs tracking this down...beyond pissed because it doesn't make sense to me.

I have a function that is activated with a button and then sets the color of the button with GUICtrlSetBkColor at the end of the function. The problem is anytime I press the enter key after I have pressed the button, the enter key activates that previous function that was activated with the button. Like the button now has focus so the enter key activates it now. Even if I try to set the focus on something else after the setbkcolor, the function still activates until I remove the setbkcolor. 

            GUISetState(@SW_LOCK, $hGUI)

            If $fHSCategories = 1 Then
                $fHSCategories = 0
                _GUICtrlListView_SetColumnWidth($cLV_1, $Col_Cat3Parent, $LVSCW_AUTOSIZE_USEHEADER)
                _GUICtrlListView_SetColumnWidth($cLV_1, $Col_Cat3Level1, $LVSCW_AUTOSIZE_USEHEADER)
                _GUICtrlListView_SetColumnWidth($cLV_1, $Col_Cat3Level2, $LVSCW_AUTOSIZE_USEHEADER)
                _GUICtrlListView_SetColumnWidth($cLV_1, $Col_CatUnisex, $LVSCW_AUTOSIZE_USEHEADER)
                _GUICtrlListView_SetColumnWidth($cLV_1, $Col_Cat1Parent, $LVSCW_AUTOSIZE_USEHEADER)
                _GUICtrlListView_SetColumnWidth($cLV_1, $Col_Cat1Level1, $LVSCW_AUTOSIZE_USEHEADER)
                _GUICtrlListView_SetColumnWidth($cLV_1, $Col_Cat1Level2, $LVSCW_AUTOSIZE_USEHEADER)

                GUICtrlSetBkColor($sHSCategories, 0xF0F0F0)

            ElseIf $fHSCategories = 0 Then

                $fHSCategories = 1
                _GUICtrlListView_SetColumnWidth($cLV_1, $Col_Cat3Parent, 0)
                _GUICtrlListView_SetColumnWidth($cLV_1, $Col_Cat3Level1, 0)
                _GUICtrlListView_SetColumnWidth($cLV_1, $Col_Cat3Level2, 0)
                _GUICtrlListView_SetColumnWidth($cLV_1, $Col_CatUnisex, 0)
                _GUICtrlListView_SetColumnWidth($cLV_1, $Col_Cat1Parent, 0)
                _GUICtrlListView_SetColumnWidth($cLV_1, $Col_Cat1Level1, 0)
                _GUICtrlListView_SetColumnWidth($cLV_1, $Col_Cat1Level2, 0)

                GUICtrlSetBkColor($sHSCategories, 0x00FF00)


            GUISetState(@SW_UNLOCK, $hGUI)

How can I fix this?

Link to comment
Share on other sites

7 hours ago, Champak said:

The problem is anytime I press the enter key after I have pressed the button, the enter key activates that previous function ...

.. that's not there. Post a running example.

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.

Link to comment
Share on other sites

  • Moderators


Setting button colours screws up something deep inside AutoIt which previous developers have said is too difficult to fix and results in the button acting strangely. Search the forum for the many earlier posts from me about this. 

Solution: do not colour buttons! 


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:


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

It seems to me the problem is not related to background color at all. If you click a button, it will then have focus so the Enter button will activate the button. Am I not understanding the problem? If that is the case, you could make a "hidden" button off the screen to accept focus after you click the button.

Would something like this work?

#include <GUIConstantsEx.au3>

Global $hGUI = GUICreate("MyGUI", 400, 350, 760, 365)
Global $Button_1 = GUICtrlCreateButton("idle", 30, 40, 125, 41)
Global $Button_2 = GUICtrlCreateButton("idle", 30, 100, 125, 41)

;create a hidden button to steal focus form other buttons
Global $Button_hidden = GUICtrlCreateButton("", -1, -1, 0, 0)


Func _main()

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE

            Case $Button_1
                GUICtrlSetData($Button_1, "clicked")
                GUICtrlSetData($Button_2, "idle")
                ConsoleWrite("button 1" & @CRLF)
                GUICtrlSetBkColor($Button_1, 0xF0F0F0)
                GUICtrlSetState($Button_hidden, $GUI_FOCUS)

            Case $Button_2
                GUICtrlSetData($Button_1, "idle")
                GUICtrlSetData($Button_2, "clicked")
                ConsoleWrite("button 2" & @CRLF)
                GUICtrlSetBkColor($Button_1, 0x00FF00)
                GUICtrlSetState($Button_hidden, $GUI_FOCUS)

            Case $Button_hidden
                ;do nothing

            Case Else

EndFunc   ;==>_main


For coloring GUI buttons, here is my take on easily setting background/foreground colors of buttons, that doesn't sacrifice functionality or appearance.


Edited by kurtykurtyboy
Link to comment
Share on other sites

No I tried that, it makes sense that it should work, but it doesn't. I set the focus to an input, you would see the curser activate in the input, but once the enter key is hit (the enter key is mapped to do something specific in my app) the button is activated again. What's bothersome is that it is a known issue and a decision was made not to correct it with no notice in the help file about it..."will cause unpredictable issues with buttons.", but there is other notes on button coloring. Oh well, on to using images. Thanks M23.

Link to comment
Share on other sites

Oh I see it now. The issue wasn't there in my example above - it only appears after adding an input control and hitting enter inside the input. Very strange.

Anyway, a quick test with the GuiFlatButton UDF linked above worked just fine. No weird behavior like with the standard buttons. That is, if you're OK with rectangular buttons.

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

  • Recently Browsing   0 members

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