Jump to content

Get Tree Child number


Recommended Posts

Hi.

I wrote below code that create tree with items and child.now I want when (only each child) is selected, show me child number with msgbox(). 

I searched in forum and find somethings but really I'm confused. if possible please guide me.

Thanks.

#RequireAdmin
#include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

;global variables
global $aStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
global $GUI
global $treeObj
global $treeItem
global $treeChild

;Create and show GUI
$GUI = GUICreate("TreeView Add", 400, 300)
GUISetState(@SW_SHOW,$GUI)

;Create Tree Object
$treeObj = GUICtrlCreateTreeView(2, 2, 300, 268,$aStyle,$WS_EX_CLIENTEDGE)

;Add Item and Child to Tree Object
for $i = 1 to 4
   $treeItem = GUICtrlCreateTreeViewItem ( "Item" & " " & $i , $treeObj )
   for $j = 1 to 4
      $treeChild = GUICtrlCreateTreeViewItem ( "Child" & " " & $j , $treeItem )
   next
Next

;loop
 While 1
     Switch GUIGetMsg()
         Case $GUI_EVENT_CLOSE
             ExitLoop
     EndSwitch
 WEnd

GUIDelete($GUI)

 

Link to comment
Share on other sites

  • Moderators

behdadsoft,

Try looking through the GuiTreeView UDF - there are a coupe of functions in there that will help you determine which item is selected and how to get its text.

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

A couple things to begin with. Not using an array in your For..Next loops to hold Ctrl Ids makes it so $treeItem and $treeChild will only hold the last item. 

Here is an example using Dummy Ctrls, with the changes to the For..Next loop:

#RequireAdmin
#include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

;global variables
global $aStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
global $GUI
global $treeObj
global $treeItem[5]
global $treeChild[5]

;Create and show GUI
$GUI = GUICreate("TreeView Add", 400, 300)
GUISetState(@SW_SHOW,$GUI)

;Create Tree Object
$treeObj = GUICtrlCreateTreeView(2, 2, 300, 268,$aStyle,$WS_EX_CLIENTEDGE)

;Add Item and Child to Tree Object
Local $iStart = GUICtrlCreateDummy()
for $i = 1 to 4 ; start off in first element of array
   $treeItem[$i] = GUICtrlCreateTreeViewItem ( "Item" & " " & $i , $treeObj )
   for $j = 1 to 4 ; start off in first element of array
      $treeChild[$j] = GUICtrlCreateTreeViewItem ( "Child" & " " & $j , $treeItem[$i] )
   next
Next
Local $iEnd = GUICtrlCreateDummy()

;loop
 While 1
     Switch GUIGetMsg()
         Case $GUI_EVENT_CLOSE
             ExitLoop
         Case $iStart To $iEnd
             MsgBox(0, "", GUICtrlRead($treeObj, 1))
     EndSwitch
 WEnd

GUIDelete($GUI)

Any use? :)

Edited by MikahS
syntax

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

Hello,

try it with _GUICtrlTreeView_GetSelection,  _GUICtrlTreeView_GetText and _GUICtrlTreeView_GetParentHandle

#RequireAdmin
#include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

;global variables
Global $aStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
Global $GUI
Global $treeObj
Global $treeItem
Global $treeChild

;Create and show GUI
$GUI = GUICreate("TreeView Add", 400, 300)
GUISetState(@SW_SHOW, $GUI)

;Create Tree Object
$treeObj = GUICtrlCreateTreeView(2, 2, 300, 268, $aStyle, $WS_EX_CLIENTEDGE)

;Add Item and Child to Tree Object
For $i = 1 To 4
    $treeItem = GUICtrlCreateTreeViewItem("Item" & " " & $i, $treeObj)
    For $j = 1 To 4
        $treeChild = GUICtrlCreateTreeViewItem("Child" & " " & $j, $treeItem)
    Next
Next

$iLastSel = 0
;loop
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
    If $iLastSel <> _GUICtrlTreeView_GetSelection($treeObj) Then
        $iLastSel = _GUICtrlTreeView_GetSelection($treeObj)
        ConsoleWrite("Parent: " & _GUICtrlTreeView_GetText($treeObj, _GUICtrlTreeView_GetParentHandle($treeObj, $iLastSel)) & @CRLF)
        ConsoleWrite("Selected: " & _GUICtrlTreeView_GetText($treeObj, $iLastSel) & @CRLF)
    EndIf
WEnd

GUIDelete($GUI)

 

greetings
bernd


I hacked 127.0.0.1 -> pcfred6.gif

Link to comment
Share on other sites

@behdadsoft a simple explanation: https://www.autoitscript.com/forum/topic/141263-can-someone-explain-gui-dummy-controls-to-me/?do=findComment&comment=993463

This will only bring up the message box for the child not items:

#RequireAdmin
#include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

;global variables
Global $aStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
Global $GUI
Global $treeObj
Global $treeItem[5]
Global $treeChild[5]

Local $treeText

;Create and show GUI
$GUI = GUICreate("TreeView Add", 400, 300)
GUISetState(@SW_SHOW, $GUI)

;Create Tree Object
$treeObj = GUICtrlCreateTreeView(2, 2, 300, 268, $aStyle, $WS_EX_CLIENTEDGE)

;Add Item and Child to Tree Object
Local $iStart = GUICtrlCreateDummy()
For $i = 1 To 4 ; start off in first element of array
    $treeItem[$i] = GUICtrlCreateTreeViewItem("Item" & " " & $i, $treeObj)
    For $j = 1 To 4 ; start off in first element of array
        $treeChild[$j] = GUICtrlCreateTreeViewItem("Child" & " " & $j, $treeItem[$i])
    Next
Next
Local $iEnd = GUICtrlCreateDummy()

;loop
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $iStart To $iEnd
            $treeText = GUICtrlRead($treeObj, 1)
            If Not StringInStr($treeText, "Item") Then
                MsgBox(0, "", $treeText)
            EndIf
    EndSwitch
WEnd

GUIDelete($GUI)

All good? :)

Edited by MikahS
spelling

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

My pleasure @behdadsoft;)

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

@MikahS

I want instead of "Item" in StringInStr($treeText, "Item") use below code for automaticaly check name. but problem is StringInStr work Vice versa.

please guide me to fix it.

;loop
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $iStart To $iEnd
            $treeText = GUICtrlRead($treeObj, 1)
            for $c = 1 to UBound($treeItem)-1
               If Not StringInStr($treeText,$treeItem[$c],0) Then
                   MsgBox(0, "", GUICtrlRead($treeObj, 1))
                EndIf
             next
    EndSwitch
WEnd

Thanks.

Link to comment
Share on other sites

My apologies @behdadsoft I did not see this reply, still working out the kinks in the new forum for notifications. :)

Here is an example as you described, that matches the selected and checks it against the child treeview items:

#RequireAdmin
#include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

;global variables
Global $aStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
Global $GUI
Global $treeObj
Global $treeItem[5]
Global $treeChild[5]

Local $treeText

;Create and show GUI
$GUI = GUICreate("TreeView Add", 400, 300)
GUISetState(@SW_SHOW, $GUI)

;~ GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

;Create Tree Object
$treeObj = GUICtrlCreateTreeView(2, 2, 300, 268, $aStyle, $WS_EX_CLIENTEDGE)

;Add Item and Child to Tree Object
Local $iStart = GUICtrlCreateDummy()
For $i = 1 To 4 ; start off in first element of array
    $treeItem[$i] = GUICtrlCreateTreeViewItem("Item" & " " & $i, $treeObj)
    For $j = 1 To 4 ; start off in first element of array
        $treeChild[$j] = GUICtrlCreateTreeViewItem("Child" & " " & $j, $treeItem[$i])
    Next
Next
Local $iEnd = GUICtrlCreateDummy()

;loop
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $iStart To $iEnd
            $treeText = GUICtrlRead($treeObj, 1)
            For $q = 1 to UBound($treeChild) -1
               If $treeText = GUICtrlRead($treeChild[$q], 1) Then
                   MsgBox(0, "", $treeText)
                   ExitLoop
                EndIf
             next
    EndSwitch
WEnd

GUIDelete($GUI)

All good? :)

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

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