Jump to content

Retaining the Absolute State of a Control...


Recommended Posts

Reading the help file, one gets the impression that GUICtrlRead() returns all a control's information, and GUICtrlGetState() returns only a subset of that same information.

Working on this assumption, to save the complete state of a control (not just its setting, but its quantitative state [Enabled/Disabled] as well), I assumed that the corollary of GUICtrlRead() would be GUICtrlSetState() - except this doesn't seem to be the case.

Hence,

$i = GUICtrlRead($CheckBox)

...

GUICtrlSetState($CheckBox, $i)

would return the control wholly to its previous condition. But, as noted, it does not.

The help file does point out that the states of a control are de facto additive, so:

$i = GUICtrlRead($CheckBox) + GuiCtrlGetState($CheckBox)

...

GUICtrlSetState($CheckBox, $i)

would seem to be the solution (indeed, it works).

So my question is, does it work because it's true, or simply because I was "lucky" with the values returned and I haven't encountered a failure yet? In other words, is this method canonically reliable?

If so, the help section for GUICtrlRead() is slightly misleading, as it suggests adding the two returned values together would "double-up" some of the states. (When GUICtrlGetState() says "this function returns ONLY the state of a control", that implies subset, not that it is the only function that DOES return that information).

Or did I read it wrong?

Link to comment
Share on other sites

  • Moderators

Kilmatead,

Reading the help file, one gets the impression that GUICtrlRead() returns all a control's information, and GUICtrlGetState() returns only a subset of that same information.

I think you read it wrong. This is my understanding of what goes on based on my experience: ;)

GUICtrlRead is used to get specific data from a control, what this is varies depending on the type of control as detailed in the table in the Help file. Note that for several controls this data can indeed be the state or a subset thereof:

- For Checkbox and Radio controls it returns only the Checked/Unchecked state (1/4) of the control.

- For Menu items it returns the full state of the control as set out in the table under GUICtrlSetState.

GUICtrlGetState will return only the enabled/disabled/hidden/show/dropaccepted state of the control as stated in the Help file - with a couple of exceptions:

- For ListView controls it returns the number of the clicked column - pretty straightforward.

- For MenuItem controls it appears not to return anything. :unsure:

Here is a short script showing the different values returned when a Checkbox and MenuItem control are clicked:

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)

$mMenu = GUICtrlCreateMenu("Menu")
$mTest = GUICtrlCreateMenuItem("Test", $mMenu)

$hCheck = GUICtrlCreateCheckbox(" Test", 10, 10)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hCheck
            ConsoleWrite("Check Read : " & GUICtrlRead($hCheck) & @CRLF)
            ConsoleWrite("Check State: " & GUICtrlGetState($hCheck) & @CRLF)
        Case $mTest
            If BitAND(GUICtrlRead($mTest), $GUI_CHECKED) = $GUI_CHECKED Then
                GUICtrlSetState($mTest, $GUI_UNCHECKED)
            Else
                GUICtrlSetState($mTest, $GUI_CHECKED)
            EndIf
            ConsoleWrite("Menu Read  : " & GUICtrlRead($mTest) & @CRLF)
            ConsoleWrite("Menu State : " & GUICtrlGetState($mTest) & @CRLF)
    EndSwitch

WEnd

I get the following results when I click the Checkbox and the Menu item twice in turn:

Check Read : 1     Checked
Check State: 80    Show + Enabled
Check Read : 4     Unchecked
Check State: 80    Show + Enabled
Menu Read  : 65    Checked + Enabled
Menu State : 0
Menu Read  : 68    UnChecked + Enabled
Menu State : 0

I can see the value of the Checkbox/Radio split (the checked/unchecked state is the most commonly required) but why why AutoIt behaves this way with MenuItem controls I have no idea - only a Dev could answer that and I have asked if one could comment.

As to the additive nature of State values, although the Help file states that they can be summed, most experienced coders use Bit level operations with them - as is obligatory with Style values. You can see how I used BitAND to extract a specific value in the script above - you would use BitOR to combine them. I have explained in the Setting Styles tutorial in the Wiki why you must do this for Styles - States can be summed arithmetically, but as you need BitAND to extract a specific value, using BitOR to combine them is a gentle hint - as well as avoiding all possibility of "doubling up" as you put it. ;)

I hope that helps. Please ask if you have any more questions - although I can offer no more detail than I already have. :D

M23

Edit: The script vanished! :>

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

Thanks for the information. I mentioned GUICtrlRead() from the point of view of the help file redirecting (for Checkbox/Radio) that it contains the state of the control, under the usual State Table reference. From this I erroneously gained my assumption that it held all said information (true, I didn't actually test for all possible states, as - well - I required sleep).

In terms of practical usage of those details, I am only too painfully aware of the necessity of BitAND'ing and BitOR'ing until I'm blue - as the GUI in question I was designing had the usual sort of thing that subsets of other commands should be GUI_DISABLE'd when the main command was unchecked, etc - so to recreate the GUI from saved information I'd have to manually check the main control first, determine if it was checked, then enable/disable (not just check/uncheck) the subcommands as necessary. It struck me as simpler to just save the absolute state of all the controls instead.

And the result was (once I added GUICtrlRead() and GUICtrlGetState() together) apparently successful. As you didn't give any caveats or admonitions against doing this, I assume it's a kosher approach?

Link to comment
Share on other sites

:>

Here is a short script showing the different values returned when a Checkbox and MenuItem control are clicked:

rofl euhmmzzz where ;)

I readed your explanation melba and is as usual good learning material :unsure:

but can't C what u mean;)

Edited by FMS

as finishing touch god created the dutch

Link to comment
Share on other sites

  • Moderators

FMS,

The script is now in the post.

I lost one of my golf balls this morning - I think the script went to the same place! :unsure:

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

  • Moderators

Kilmatead,

a kosher approach

I assume so, although I would still BitOR rather than add. :>

As I said earlier I have asked for a Dev to take a look and see if any more light can be shed on the apparently anomalous behaviour concerning MenuItem controls. I imagine it is a hangover from the early days of Autoit and has stayed this way for backwards compatibility reasons. :unsure:

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

In the early days of AutoIt we didn't have a GUI incorporated into it. We had a separate app written by Larry to create GUIs. Larry worked the code into AutoIt (perhaps with help?) and after that it was the devs at the time that maintained it.

Valik has stated several times that the GUI code is a mess and I think this is just one of those anomalies.

Here is another demo script That also shows how to use GUICtrlRead() to get the text of the menu control. Warning; it works fine in a message box and I think in an edit control but I have had a problem using it to set text in a combo.

#Include<GuiConstantsEx.au3>
GUICreate("")
$hMenu = GUICtrlCreateMenu("File")
$hTestMenu = GUICtrlCreateMenuItem("Test", $hMenu)
$hTestMenu2 = GUICtrlCreateMenuItem("Test 2", $hMenu)
$hExitMenu = GUICtrlCreateMenuItem("Exit", $hMenu)
GUISetState()

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE, $hExitMenu
            Exit
        Case $hTestMenu, $hTestMenu2
            _CheckToggle($Msg)
            MsgBox(0, "Test 1", "GUICtrlRead = " & GUICtrlRead($Msg))
            MsgBox(0, "Test 2", "GUICtrlGetState = " & GUICtrlGetState($Msg))
            MsgBox(0, "Test 3", "Control Text = " & GUICtrlRead($Msg, 1))
    EndSwitch
WEnd

Func _CheckToggle($h_Ctrl)
    If BitAND(GUICtrlRead($h_Ctrl), $GUI_CHECKED) = $GUI_CHECKED Then
        GUICtrlSetState($h_Ctrl, $GUI_UNCHECKED)
        Return
    EndIf
    GUICtrlSetState($h_Ctrl, $GUI_CHECKED)
EndFunc

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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