Jump to content

TreeView - Keep current item selected


 Share

Recommended Posts

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

Link to comment
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

 

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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

 

Link to comment
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

 

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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.

Link to comment
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.

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