Jump to content

TreeVIew Check only Child Selected


Recommended Posts

Hi.

I wrote below code that contain some parent and child. now i want when i click on only child, give me a message. but problem is when i click on parent, also give me message.

dose anyone know how can fix this problem?

 

#RequireAdmin
#include <File.au3>
#include <Array.au3>
#include <IE.au3>
#include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

;variables
Global $GUI
Global $TreeParent[3]
Global $TreeChild[4]
Global $aStyle = BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)

;Create GUI Form
$GUI = GUICreate("TreeView", 300, 428)
GUISetState(@SW_SHOW, $GUI)

;Create TreeView
Local $TreeObject = GUICtrlCreateTreeView(0, 0, 300, 428,$aStyle, $WS_EX_CLIENTEDGE)

;Get SubFolders and Add Child to Tree View Item
Local $iStart = GUICtrlCreateDummy()
   for $a = 1 to UBound($TreeParent) - 1
      $TreeParent[$a] = GUICtrlCreateTreeViewItem("Parent " & $a,$TreeObject)
      for $b = 1 to UBound($TreeChild) - 1
         $TreeChild[$b] = GUICtrlCreateTreeViewItem("Child " & $b ,$TreeParent[$a])
      Next
   Next
Local $iEnd = GUICtrlCreateDummy()

;Repeat Show GUI
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $iStart To $iEnd
            $treeText = GUICtrlRead($TreeObject, 1)
            For $c = 1 to UBound($TreeChild) - 1
               If $treeText = GUICtrlRead($TreeChild[$c]) Then
                  MsgBox(0,"", $treeText)
                  ExitLoop
               EndIf
             next
    EndSwitch
WEnd

GUIDelete($GUI)

 

Link to comment
Share on other sites

I found it. :)

#RequireAdmin
#include <File.au3>
#include <Array.au3>
#include <IE.au3>
#include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

;variables
Global $GUI
Global $TreeParent[3]
Global $TreeChild[4]
Global $aStyle = BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)

;Create GUI Form
$GUI = GUICreate("TreeView", 300, 428)
GUISetState(@SW_SHOW, $GUI)

;Create TreeView
Local $TreeObject = GUICtrlCreateTreeView(0, 0, 300, 428,$aStyle, $WS_EX_CLIENTEDGE)

;Get SubFolders and Add Child to Tree View Item
Local $iStart = GUICtrlCreateDummy()
   for $a = 1 to UBound($TreeParent) - 1
      $TreeParent[$a] = GUICtrlCreateTreeViewItem("Parent " & $a,$TreeObject)
      for $b = 1 to UBound($TreeChild) - 1
         $TreeChild[$b] = GUICtrlCreateTreeViewItem("Child " & $b ,$TreeParent[$a])
      Next
   Next
Local $iEnd = GUICtrlCreateDummy()

;Repeat Show GUI
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $iStart To $iEnd
            $treeText = GUICtrlRead($TreeObject, 1)
            For $c = 1 to UBound($TreeChild) - 1
               If $treeText = GUICtrlRead($TreeChild[$c], 1) Then
                  MsgBox(0,"", $treeText)
                  ExitLoop
               EndIf
             next
    EndSwitch
WEnd

GUIDelete($GUI)

 

Edited by behdadsoft
Link to comment
Share on other sites

Now, in this part of above code i want instead of use strings ("Parent" and "Child"), use record folder in Script directory.

;Get SubFolders and Add Child to Tree View Item
Local $iStart = GUICtrlCreateDummy()
   for $a = 1 to UBound($TreeParent) - 1
      $TreeParent[$a] = GUICtrlCreateTreeViewItem("Parent " & $a,$TreeObject)
      for $b = 1 to UBound($TreeChild) - 1
         $TreeChild[$b] = GUICtrlCreateTreeViewItem("Child " & $b ,$TreeParent[$a])
      Next
   Next
Local $iEnd = GUICtrlCreateDummy()

This is new code :

#RequireAdmin
#include <File.au3>
#include <Array.au3>
#include <IE.au3>
#include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

;variables
Global Const $Path = @ScriptDir
Global $GUI
Global $RecordChildFolder
Global $RecordFiles
Global $GetFolderList
Dim $TreeParent[1]
Dim $TreeChild[7]
Global $aStyle = BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)

;Create GUI Form
$GUI = GUICreate("Autorun", 800, 600)
GUISetState(@SW_SHOW, $GUI)

;Create TreeView
Local $TreeObject = GUICtrlCreateTreeView(2, 170, 300, 428,$aStyle, $WS_EX_CLIENTEDGE)

;Get Folder name to Array
$GetFolderList = _FileListToArray($Path,"*",$FLTA_FOLDERS)

ReDim $TreeParent[UBound($GetFolderList)]

;Get SubFolders and Add Child to Tree View Item
Local $iStart = GUICtrlCreateDummy()
   for $a = 1 to UBound($TreeParent) - 1
      $TreeParent[$a] = GUICtrlCreateTreeViewItem($GetFolderList[$a],$TreeObject)
      $RecordChildFolder = _FileListToArrayRec($GetFolderList[$a],"*",$FLTAR_FOLDERS,$FLTAR_RECUR,$FLTAR_SORT)
      for $b = 1 to UBound($RecordChildFolder) - 1
         $TreeChild[$b] = GUICtrlCreateTreeViewItem($RecordChildFolder[$b] ,$TreeParent[$a])
      Next
   Next
Local $iEnd = GUICtrlCreateDummy()

;Repeat Show GUI
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $iStart To $iEnd
            $treeText = GUICtrlRead($TreeObject, 1)
            For $q = 1 to UBound($TreeChild) - 1
               If $treeText = GUICtrlRead($TreeChild[$q], 1) Then
                  MsgBox(64,"", $treeText)
                  ExitLoop
               EndIf
             next
    EndSwitch
WEnd

GUIDelete($GUI)

Now I have same problem in like my first post. (when i click on parent and child objects, give message. while i only want when i click on child, give me a message).

Edited by behdadsoft
Link to comment
Share on other sites

You do it this way:

#RequireAdmin
#include <File.au3>
#include <Array.au3>
#include <IE.au3>
#include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

;variables
Global $GUI
Global $TreeParent[3]
Global $TreeChild[6], $iChilds = 0
Global $aStyle = BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)

;Create GUI Form
$GUI = GUICreate("TreeView", 300, 428)
GUISetState(@SW_SHOW, $GUI)

;Create TreeView
Local $TreeObject = GUICtrlCreateTreeView(0, 0, 300, 428,$aStyle, $WS_EX_CLIENTEDGE)

;Get SubFolders and Add Child to Tree View Item
Local $iStart = GUICtrlCreateDummy()
   for $a = 1 to UBound($TreeParent) - 1
      $TreeParent[$a] = GUICtrlCreateTreeViewItem("Parent " & $a,$TreeObject)
      for $b = 0 to UBound($TreeChild) / 2 - 1
         $TreeChild[$iChilds] = GUICtrlCreateTreeViewItem("Child " & $iChilds ,$TreeParent[$a])
         $iChilds += 1
      Next
   Next
Local $iEnd = GUICtrlCreateDummy()

;Repeat Show GUI
While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $iStart + 1 To $iEnd - 1
            For $c = 0 to $iChilds - 1
               If $iMsg = $TreeChild[$c] Then
                  MsgBox(0,"", GUICtrlRead($iMsg, 1))
                  ExitLoop
               EndIf
             next
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

GUIDelete($GUI)

 

Link to comment
Share on other sites

Thanks 

I was fixed above code in this block:

For $q = 0 to UBound($TreeChild) - 1
   If $treeText = GUICtrlRead($TreeChild[$q], 1) Then
      MsgBox(64,"", $treeText)
      ExitLoop
   EndIf
next

in this code i don't have same parent and child, if possible please tell me how can fix this problem in new code with my own way.

#RequireAdmin
#include <File.au3>
#include <Array.au3>
#include <IE.au3>
#include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

;variables
Global Const $Path = @ScriptDir
Global $GUI
Global $RecordChildFolder
Global $RecordFiles
Global $GetFolderList
Dim $TreeParent[1]
Dim $TreeChild[7]
Global $aStyle = BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)

;Create GUI Form
$GUI = GUICreate("Autorun", 800, 600)
GUISetState(@SW_SHOW, $GUI)

;Create TreeView
Local $TreeObject = GUICtrlCreateTreeView(2, 170, 300, 428,$aStyle, $WS_EX_CLIENTEDGE)

;Get Folder name to Array
$GetFolderList = _FileListToArray($Path,"*",$FLTA_FOLDERS)

ReDim $TreeParent[UBound($GetFolderList)]

;Get SubFolders and Add Child to Tree View Item
Local $iStart = GUICtrlCreateDummy()
   for $a = 1 to UBound($TreeParent) - 1
      $TreeParent[$a] = GUICtrlCreateTreeViewItem($GetFolderList[$a],$TreeObject)
      $RecordChildFolder = _FileListToArrayRec($GetFolderList[$a],"*",$FLTAR_FOLDERS,$FLTAR_RECUR,$FLTAR_SORT)
      for $b = 1 to UBound($RecordChildFolder) - 1
         $TreeChild[$b] = GUICtrlCreateTreeViewItem($RecordChildFolder[$b] ,$TreeParent[$a])
      Next
   Next
Local $iEnd = GUICtrlCreateDummy()

;Repeat Show GUI
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $iStart To $iEnd
            $treeText = GUICtrlRead($TreeObject, 1)
            For $q = 1 to UBound($TreeChild) - 1
               If $treeText = GUICtrlRead($TreeChild[$q], 1) Then
                  MsgBox(64,"", $treeText)
                  ExitLoop
               EndIf
             next
    EndSwitch
WEnd

GUIDelete($GUI)

 

Link to comment
Share on other sites

When it comes to reading a folder structure, more efficient techniques should be used because there can be many folders. Here the item level in the treeview is calculated. If the level is larger than 1 it's not a top folder:

#RequireAdmin
#include <File.au3>
#include <Array.au3>
#include <IE.au3>
#include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

;variables
Global Const $Path = @ScriptDir
Global $GUI
Global $RecordChildFolder
Global $RecordFiles
Global $GetFolderList
Dim $TreeParent[1]
Dim $TreeChild[100], $iChilds = 0
Global $aStyle = BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)

;Create GUI Form
$GUI = GUICreate("Autorun", 800, 600)
GUISetState(@SW_SHOW, $GUI)

;Create TreeView
Local $TreeObject = GUICtrlCreateTreeView(2, 170, 300, 428,$aStyle, $WS_EX_CLIENTEDGE)
$hTreeObject = GUICtrlGetHandle( $TreeObject )

;Get Folder name to Array
$GetFolderList = _FileListToArray($Path,"*",$FLTA_FOLDERS)

ReDim $TreeParent[UBound($GetFolderList)]

;Get SubFolders and Add Child to Tree View Item
Local $iStart = GUICtrlCreateDummy()
   for $a = 1 to UBound($TreeParent) - 1
      $TreeParent[$a] = GUICtrlCreateTreeViewItem($GetFolderList[$a],$TreeObject)
      $RecordChildFolder = _FileListToArrayRec($GetFolderList[$a],"*",$FLTAR_FOLDERS,$FLTAR_RECUR,$FLTAR_SORT)
      for $b = 1 to UBound($RecordChildFolder) - 1
         $TreeChild[$b] = GUICtrlCreateTreeViewItem($RecordChildFolder[$b] ,$TreeParent[$a])
         $iChilds += 1
      Next
   Next
Local $iEnd = GUICtrlCreateDummy()

;Repeat Show GUI
While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $iStart - 1 To $iEnd - 1
          ; Calculate item treeview level
          Local $iLevel = 0, $hItem = GUICtrlGetHandle( $iMsg )
          While $hItem And $iLevel < 2
            $hItem = _SendMessage( $hTreeObject, $TVM_GETNEXTITEM, $TVGN_PARENT, $hItem, 0, "wparam", "handle", "handle")
            $iLevel += 1
          WEnd
          ; If item treeview level > 1 it's not a top folder
          If $iLevel > 1 Then MsgBox(0,"", GUICtrlRead($iMsg, 1))
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

GUIDelete($GUI)

 

Link to comment
Share on other sites

It should be:

Case $iStart + 1 To $iEnd - 1

You are only interested in treeview item controls. $iStart and $iEnd are dummy controls, so let's exclude both.

 

WUsYnrp.png

The level is the number of vertical lines in front of a treeview item. Here all parent items are level 1 and all child items are level 2.

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