Sign in to follow this  
Followers 0
syntech

How to detect checkbox changes in a TreeView?

10 posts in this topic

Hello!

I would like to know if it is possible to detect checkbox changes in a TreeView without checking every single TreeViewItem? Or how can I find out if a TreeView has the focus? At the moment I'm consistently checking all TreeViewItems for changes, but this is very bad for the performance. That's why I would like to know if the TreeView is in use or not, because if not, then I can skip the iteration through all items and do it only when the user is actually 'using' the TreeView. Any suggestions?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Thanks for your suggestion. I've never used Events till now, so I'll have a look at it.

Share this post


Link to post
Share on other sites

Take a look at the _GUICtrlTreeView_Create() example in the help-file (note that you'll have to replace some const figures by adding an A if they're found undeclared: e.g. $TVN_DELETEITEM => $TVN_DELETEITEMA). Using WM_NOTIFY you can detect if the treeview has been accessed and what has been done. As far as I can see these two:

$NM_CLICK

$TVN_KEYDOWN

can be used to change checkbox state, either by mouse or keyboard. If you detect one of them, issue a _GUICtrlTreeView_GetChecked() command on the respective control handle and save the states to an array representing your treeview states... no need to loop anymore :D...

Share this post


Link to post
Share on other sites

[..]Using WM_NOTIFY you can detect if the treeview has been accessed and what has been done. As far as I can see these two:

$NM_CLICK

$TVN_KEYDOWN

can be used to change checkbox state, either by mouse or keyboard. If you detect one of them, issue a _GUICtrlTreeView_GetChecked() command on the respective control handle and save the states to an array representing your treeview states... no need to loop anymore :D...

This sounds very intersting, too! Thanks also to you for your suggestion. I've never used WM_NOTIFY before, so I'll check what it can do for me. :huggles:

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

This is strange... Is _GUICtrlTreeView_GetChecked() broken or returns true in case of false and vice versa? Because when I check the box and output the return value of _GUICtrlTreeView_GetChecked() it says false. When I de-check the box it returns true... According to the help file it should return true for checked boxes and false for unchecked boxes. Is this a known issue?

edit: I'm using Version 3.3.4.0 (the latest release)

Edited by syntech

Share this post


Link to post
Share on other sites

Works fine for me...

#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>

Local $hItem[6], $hRandomItem, $hTreeView, $edit, $string
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)

GUICreate("TreeView Get Checked", 620, 300)

$edit = GUICtrlCreateEdit("", 400, 10, 200)

$hTreeView = GUICtrlCreateTreeView(2, 2, 396, 200, $iStyle, $WS_EX_CLIENTEDGE)
GUISetState()

_GUICtrlTreeView_BeginUpdate($hTreeView)
For $x = 0 To UBound($hItem) - 1
    $hItem[$x] = GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Item", $x + 1), $hTreeView)
Next
_GUICtrlTreeView_EndUpdate($hTreeView)

; Loop until user exits
Do
    Sleep(500)
    $string = ""
    For $x = 0 To UBound($hItem) - 1
        $string &= "Item " & $x & " is checked: " & _GUICtrlTreeView_GetChecked($hTreeView, $hItem[$x]) & @CRLF
    Next
    GUICtrlSetData($edit, $string)
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()

Share this post


Link to post
Share on other sites

@KaFu:

I found the problem. I was checking to early. I added the test to the $NM_CLICK case, but at this time the checkbox is not really set yet. Now I have to find out when I can add my routine, because $TVN_SELCHANGE does not work either, because I can select the checkbox of item 2 but item 1 is still highlighted, therefore no selection change has happend and the case is not true. I'll look if there is a possibility to make a selection change when I only click into the checkbox. Then it should work I hope.

Share this post


Link to post
Share on other sites

I took a deeper look... and it's much more complicated then I anticipated in the beginning :D... take a look into my ShellTristateTreeView (see sig), there I use some code based on Holgers work to check the states.

Is it really necessary to monitor each change? Alternatively you could wait for the message $NM_KILLFOCUS and then loop through all items to check if they're checked.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

[..]Is it really necessary to monitor each change? Alternatively you could wait for the message $NM_KILLFOCUS and then loop through all items to check if they're checked.

Well, I wanted to react to every checked TreeViewItem the moment it is checked/unchecked. But I'll try it with $NM_KILLFOCUS, maybe it's good enough... Edited by syntech

Share this post


Link to post
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
Sign in to follow this  
Followers 0