DarkBanana

TreeView - Keep current item selected

10 posts in this topic

#include <Array.au3>
#include <TreeViewConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    GUICreate("My GUICtrlRead")

    $TreeVIew = GUICtrlCreateTreeView(10,10,200,200) ; $TVS_SHOWSELALWAYS doesn't work as intended
    $1 = GUICtrlCreateTreeViewItem("New",$TreeView)
    $2 = GUICtrlCreateTreeViewItem("Tree",$1)
    GUICtrlCreateTreeViewItem("Leaf",$2)
    GUISetState(@SW_SHOW)


    GUICtrlCreateDate("",10,250,100,100)
    Do
        $idMsg = GUIGetMsg()

    Until $idMsg = $GUI_EVENT_CLOSE
EndFunc

Hey there,

I would like to know how to keep my treeview selection if my treeview control loses focus. 

I tried $TVS_SHOWSELALWAYS but it doesn't seem to work.

And is it possible to keep my selection even if I change to another child gui?

 

Thanks in advance.

Share this post


Link to post
Share on other sites



It is still selected, it just loses focus on the control. Which, greys it out and does not lose the selection. If the window is given focus again it will be on the same selection.

Untitled.thumb.png.1e6d47f7766348a034fb6


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

Well thanks for clarifying.

I would like to keep the blue selection. So I can clearly see what I have selected previously.

Is there a nice solution for this. Or do I need to use a workaround?

Share this post


Link to post
Share on other sites

I wouldn't use a workaround. Greying out a selection in a listview without focus is the way Windows works. And it is what users have gotten used to.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

My pleasure and I would agree with @water on this one. The only way I have seen anyone modify this is with VB or C#.


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

I don't really care what "users" have gotten used to. So I need to use a workaround.

Let's see where it takes me.

Thanks anyway.

Share this post


Link to post
Share on other sites

Have a look at this link.


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

I don't really care what "users" have gotten used to.

Let me reword it: Greying out the control is a standard set up by Microsoft. So why change it?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
#include <Array.au3>
#include <TreeViewConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <GuiTreeView.au3>
#include <ColorConstants.au3>
#include <WindowsConstants.au3>
Example()

Func Example()
    GUICreate("My GUICtrlRead")
    $CurSel = ""
    Global $TreeVIew = GUICtrlCreateTreeView(10,10,200,200) ; $TVS_SHOWSELALWAYS doesn't work as intended
    Global $1 = GUICtrlCreateTreeViewItem("New",$TreeView)
    Global $2 = GUICtrlCreateTreeViewItem("Tree",$1)
    GUICtrlCreateTreeViewItem("Leaf",$2)
    GUICtrlCreateButton("",10,250,100,100)
    GUICtrlCreateInput("",250,10,100,100)
    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    GUISetState(@SW_SHOW)



    While 1
        $msg = GUIGetMsg()

        Switch $msg
            Case -3
                Exit
        EndSwitch

    WEnd
EndFunc


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $TreeView
    If Not IsHWnd($TreeView) Then $hWndTreeview = GUICtrlGetHandle($TreeView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndTreeview
            Switch $iCode
                Case $TVN_SELCHANGEDA, $TVN_SELCHANGEDW


                Case $TVN_SELCHANGINGA, $TVN_SELCHANGINGW

                Case $NM_KILLFOCUS
                    $Selected = _GUICtrlTreeView_GetSelection($TreeView)

                    GUICtrlSetBkColor($1,0x3399ff)  ; GUICtrlSetBkColor($Selected,0x3399ff) doesn't work
                    GUICtrlSetColor($1,0xFFFFFF)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

This is what I got so far. But I can't change the background color of the currently selected item. The handle returned by ..._GetSelection doesn't seem to work.

And in the TreeView UDF there's no function to change the color of a specific item or is there?

@Water 

I want the bright blue color so I can clearly see what is selected. I can barely see the grey background color. That's why I want to change it.

Share this post


Link to post
Share on other sites

The color of the selected item is controlled by Windows. With and without focus. Remove the selected state of the item and you can set the color yourself.

You need the item controlId to set the color with GUICtrlSetBkColor. Not the item handle.

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