Jump to content
Sign in to follow this  
behdadsoft

[Solved] Make simple Search

Recommended Posts

behdadsoft
Posted (edited)

Hi.

I have this code that contain parent and child in TreeViewObject. now i have an Inuputbox and i want if wrote everything in it, compare with child name and if they are equal, only show current child with itself parent and hide other parent and items. dose anyone can help to do this?

This is my code:

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

local Const $Path = @ScriptDir
Dim $Parent[3]
Dim $Child[2]
local $Count = 0

;Create GUI
local $GUI = GUICreate("TreeView")
GUISetState(@SW_SHOW)

; Create InputBox
local $Input = GUICtrlCreateInput("",100,25)
local $InputText = GUICtrlRead($Input,1) ;Read InputBox Text

; Crete TreeViewObject
local $TreeObject = GUICtrlCreateTreeView(100,70,200,250)


;Create Tree Parent
for $i = 0 to UBound($Parent) - 1
   $Parent[$i] = GUICtrlCreateTreeViewItem("Parent " & $i, $TreeObject)
      ;Add Child to Parents
   for $j = 0 to UBound($Child) - 1
      $Child[$j] = GUICtrlCreateTreeViewItem("Child " & $Count, $Parent[$i])
      $Count += 1
   Next
Next

while 1

   Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
         ExitLoop

      ; Search in InputBox
      Case $InputText
         if $InputText <> "" Then
            for $i = 0 to UBound($Child) - 1
               MsgBox(0,"",$InputText)
               if StringInStr($Child[$i], $InputText) <> 0 Then
                  ;Show only Current child and parent that are equal with InputText
               Else
                  ;Show all Parent and child
               EndIf
            Next
         EndIf
   EndSwitch

WEnd

GUIDelete($GUI)

Thanks.

Edited by behdadsoft

Share this post


Link to post
Share on other sites
LarsJ

behdadsoft, This is your code with some comments:

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

local Const $Path = @ScriptDir
Dim $Parent[3]
Dim $Child[2]
local $Count = 0

;Create GUI
local $GUI = GUICreate("TreeView")
GUISetState(@SW_SHOW)

; Create InputBox
local $Input = GUICtrlCreateInput("",100,25)
; local $InputText = GUICtrlRead($Input,1) ;Read InputBox Text ; Makes no sense at this point in code

; Crete TreeViewObject
local $TreeObject = GUICtrlCreateTreeView(100,70,200,250)


;Create Tree Parent
for $i = 0 to UBound($Parent) - 1
   $Parent[$i] = GUICtrlCreateTreeViewItem("Parent " & $i, $TreeObject)
      ;Add Child to Parents
   for $j = 0 to UBound($Child) - 1 ; This loop owerwrites previous items as you can see with _ArrayDisplay
      $Child[$j] = GUICtrlCreateTreeViewItem("Child " & $Count, $Parent[$i]) ; Returns item control ID, not item text
      $Count += 1
   Next
   _ArrayDisplay( $Child, "$Child" )
Next

while 1

   Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
         ExitLoop

      ; Search in InputBox
      ;Case $InputText
      Case $Input
         $InputText = GUICtrlRead($Input)
         if $InputText <> "" Then
            MsgBox(0,"",$InputText)
            for $i = 0 to UBound($Child) - 1
               if StringInStr($Child[$i], $InputText) <> 0 Then
                  ; $Child contains item control IDs, makes no sense in this context
                  ;Show only Current shild and parent that are equal with InputText
               Else
                  ;Show all Parent and child
               EndIf
            Next
         EndIf
   EndSwitch

WEnd

GUIDelete($GUI)

 

Then you can continue working with this code:

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

local Const $Path = @ScriptDir
Dim $Parent[3]
Dim $Child[6]
local $Count = 0

;Create GUI
local $GUI = GUICreate("TreeView")
GUISetState(@SW_SHOW)

; Create InputBox
local $Input = GUICtrlCreateInput("",100,25)

; Crete TreeViewObject
local $TreeObject = GUICtrlCreateTreeView(100,70,200,250)


;Create Tree Parent
for $i = 0 to UBound($Parent) - 1
   $Parent[$i] = GUICtrlCreateTreeViewItem("Parent " & $i, $TreeObject)
   ;Add Child to Parents
   for $j = 0 to 1
      $Child[$Count] = GUICtrlCreateTreeViewItem("Child " & $Count, $Parent[$i])
      $Count += 1
   Next
   _ArrayDisplay( $Child, "$Child" )
Next

while 1

   Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
         ExitLoop

      ; Search in InputBox
      ; Search for 1 - 6
      Case $Input
         $InputText = GUICtrlRead($Input)
         if $InputText <> "" Then
            MsgBox(0,"",$InputText)
            for $i = 0 to UBound($Child) - 1
               if StringInStr(GUICtrlRead($Child[$i],1), $InputText) <> 0 Then
                  MsgBox(0,"",GUICtrlRead($Child[$i],1))
                  ;Show only Current shild and parent that are equal with InputText
               Else
                  ;Show all Parent and child
               EndIf
            Next
         EndIf
   EndSwitch

WEnd

GUIDelete($GUI)

 

Add a new post if you have any doubts.

  • Thanks 1

Share this post


Link to post
Share on other sites
behdadsoft

Thanks LarsJ.

Currently i want after write context, don't need to press enter for begin compare. i want compare happening during write into Inputbox.

In this part of code i used Case GUICtrlRead($Input,1) instead of Case $Input. but when result is equal, program give me unlimited Message. how can fix this?

while 1

   Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
         ExitLoop

      ; Search in InputBox
      ; Search for 1 - 6
      Case GUICtrlRead($Input,1)
         $InputText = GUICtrlRead($Input)
         if $InputText <> "" Then
            ;MsgBox(0,"",$InputText)
            for $i = 0 to UBound($Child) - 1
               if StringInStr(GUICtrlRead($Child[$i],1), $InputText) <> 0 and StringLen($InputText) > 4 Then
                  MsgBox(0,"",GUICtrlRead($Child[$i],1))
                  ExitLoop
                  ;Show only Current shild and parent that are equal with InputText
               Else
                  ;Show all Parent and child
               EndIf
            Next
         EndIf
   EndSwitch

WEnd

 

Share this post


Link to post
Share on other sites
LarsJ

A comment to your code: You cannot use GUICtrlRead() in a Case statement in the message loop. You can only use control IDs and the 11 system events.

So you want incremental search. That's more complicated. You need a WM_COMMAND message handler. See this section in the Wiki.

I've used ConsoleWrites in the code, so run the code in SciTE with F5:

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

local Const $Path = @ScriptDir
Dim $Parent[3]
Dim $Child[15]
local $Count = 0

;Create GUI
local $GUI = GUICreate("TreeView")
GUISetState(@SW_SHOW)

; Create InputBox
local $Input = GUICtrlCreateInput("",100,25)
Local $idInputChange = GUICtrlCreateDummy()

; Crete TreeViewObject
local $TreeObject = GUICtrlCreateTreeView(100,70,200,250)


;Create Tree Parent
for $i = 0 to UBound($Parent) - 1
   $Parent[$i] = GUICtrlCreateTreeViewItem("Parent " & $i, $TreeObject)
   ConsoleWrite( "Parent " & $i & @CRLF )
   ;Add Child to Parents
   for $j = 0 to 4
      $Child[$Count] = GUICtrlCreateTreeViewItem("Child " & $Count, $Parent[$i])
      ConsoleWrite( "    Child " & $Count & @CRLF )
      $Count += 1
   Next
  ConsoleWrite( @CRLF )
Next

ConsoleWrite( "Search for 0 - 14" & @CRLF & @CRLF )

GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")

while 1

   Switch GUIGetMsg()
      ; Search in InputBox
      ; Search for 0 - 14
      Case $idInputChange
         $InputText = GUICtrlRead($Input)
         if $InputText <> "" Then
            ConsoleWrite( "$InputText = [" & $InputText & "]" & @CRLF )
            Local $iMatches = 0, $sMatches = ""
            for $i = 0 to UBound($Child) - 1
               if StringInStr(GUICtrlRead($Child[$i],1), $InputText) <> 0 Then
                  $iMatches += 1
                  $sMatches &= GUICtrlRead($Child[$i],1) & @CRLF
               EndIf
            Next
            ConsoleWrite( "Number of matches = " & $iMatches & @CRLF )
            If $sMatches Then ConsoleWrite( $sMatches )
            ConsoleWrite( @CRLF )
         EndIf

      Case $GUI_EVENT_CLOSE
         ExitLoop
   EndSwitch

WEnd

GUIDelete($GUI)

Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
  Local $iIDFrom = BitAND($wParam, 0xFFFF) ; LoWord - this gives the control which sent the message
  Local $iCode = BitShift($wParam, 16)     ; HiWord - this gives the message that was sent
  If $iCode = $EN_CHANGE Then ; If we have the correct message
    Switch $iIDFrom ; See if it comes from one of the inputs
      Case $Input
        GUICtrlSendToDummy( $idInputChange )
    EndSwitch
  EndIf
EndFunc ;==>MY_WM_COMMAND

 

  • Thanks 1

Share this post


Link to post
Share on other sites
behdadsoft

Thanks LarsJ.

If there is any question, ask here. :)

Share this post


Link to post
Share on other sites
behdadsoft
Posted (edited)

I need guide:

with below code i can get selected parent of selected child in Treeview.

Local $handle = _GUICtrlTreeView_GetParentHandle($TreeObject, GUICtrlRead($h_TreeObject, 1))
local $ParentName = _GUICtrlTreeView_GetText($TreeObject, $handle)

but above code don't work for input box and i know why it don't work. because don't selected any child in treeview. 

Local $handle = _GUICtrlTreeView_GetParentHandle($TreeObject, GUICtrlRead($sMatches, 1))
local $ParentName = _GUICtrlTreeView_GetText($TreeObject, $handle)

i want when write something in inputbox, according below code i get parent name of $sMatches.

$sMatches &= GUICtrlRead($Child[$i],1) & @CRLF

how can get parent name of matches items?

Edited by behdadsoft

Share this post


Link to post
Share on other sites
LarsJ

You do it this way:

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

local Const $Path = @ScriptDir
Dim $Parent[3]
Dim $Child[15]
local $Count = 0

;Create GUI
local $GUI = GUICreate("TreeView")
GUISetState(@SW_SHOW)

; Create InputBox
local $Input = GUICtrlCreateInput("",100,25)
Local $idInputChange = GUICtrlCreateDummy()

; Crete TreeViewObject
local $TreeObject = GUICtrlCreateTreeView(100,70,200,250)


;Create Tree Parent
for $i = 0 to UBound($Parent) - 1
   $Parent[$i] = GUICtrlCreateTreeViewItem("Parent " & $i, $TreeObject)
   ConsoleWrite( "Parent " & $i & @CRLF )
   ;Add Child to Parents
   for $j = 0 to 4
      $Child[$Count] = GUICtrlCreateTreeViewItem("Child " & $Count, $Parent[$i])
      ConsoleWrite( "    Child " & $Count & @CRLF )
      $Count += 1
   Next
  ConsoleWrite( @CRLF )
Next

ConsoleWrite( "Search for 0 - 14" & @CRLF & @CRLF )

GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")

while 1

   Switch GUIGetMsg()
      ; Search in InputBox
      ; Search for 0 - 14
      Case $idInputChange
         $InputText = GUICtrlRead($Input)
         if $InputText <> "" Then
            ConsoleWrite( "$InputText = [" & $InputText & "]" & @CRLF )
            Local $sParent, $aMatches[15], $iMatches = 0
            for $i = 0 to UBound($Child) - 1
               if StringInStr(GUICtrlRead($Child[$i],1), $InputText) <> 0 Then
                  $sParent = _GUICtrlTreeView_GetText($TreeObject, _GUICtrlTreeView_GetParentHandle($TreeObject, $Child[$i]))
                  For $j = 0 To $iMatches - 1
                    If $aMatches[$j] = $sParent Then ExitLoop
                  Next
                  If $j = $iMatches Then
                    ; Parent not found, add parent
                    $aMatches[$iMatches] = $sParent
                    $iMatches += 1
                  EndIf
               EndIf
            Next
            ConsoleWrite( "Number of parent matches = " & $iMatches & @CRLF )
            If $iMatches Then
              For $j = 0 To $iMatches - 1
                ConsoleWrite( $aMatches[$j] & @CRLF )
              Next
            EndIf
            ConsoleWrite( @CRLF )
         EndIf

      Case $GUI_EVENT_CLOSE
         ExitLoop
   EndSwitch

WEnd

GUIDelete($GUI)

Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
  Local $iIDFrom = BitAND($wParam, 0xFFFF) ; LoWord - this gives the control which sent the message
  Local $iCode = BitShift($wParam, 16)     ; HiWord - this gives the message that was sent
  If $iCode = $EN_CHANGE Then ; If we have the correct message
    Switch $iIDFrom ; See if it comes from one of the inputs
      Case $Input
        GUICtrlSendToDummy( $idInputChange )
    EndSwitch
  EndIf
EndFunc ;==>MY_WM_COMMAND

 

  • Like 1

Share this post


Link to post
Share on other sites
behdadsoft

Thanks LarsJ:)

Share this post


Link to post
Share on other sites
behdadsoft
Posted (edited)

@LarsJ

I Finished it and search is work very well. ^_^

Thanks for your help. :)

Edited by behdadsoft

Share this post


Link to post
Share on other sites
behdadsoft
Posted (edited)

@LarsJ

There is a problem in search with multi GUICtrlCreateDummy() in my code.

when i mixed search code with my own code, that before you have seen it at below topic, search don't work.

https://www.autoitscript.com/forum/topic/194154-treeview-check-only-child-selected/?tab=comments#comment-1392722

Because In my code there are two Dummy and when i add this piece of code,  search don't work.

; Create InputBox
local $Input = GUICtrlCreateInput("",100,25)
Local $idInputChange = GUICtrlCreateDummy()

Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
  Local $iIDFrom = BitAND($wParam, 0xFFFF) ; LoWord - this gives the control which sent the message
  Local $iCode = BitShift($wParam, 16)     ; HiWord - this gives the message that was sent
  If $iCode = $EN_CHANGE Then ; If we have the correct message
    Switch $iIDFrom ; See if it comes from one of the inputs
      Case $InputBox
        GUICtrlSendToDummy( $idInputChange )
    EndSwitch
  EndIf
EndFunc ;==>MY_WM_COMMAND

 

;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()

do you have any idea?

Edited by behdadsoft

Share this post


Link to post
Share on other sites
behdadsoft

I fixed it. :)

Share this post


Link to post
Share on other sites
Earthshine
Posted (edited)

can you post your code fixed code and mark this thread as Solved? you can edit the first post and add Solved to your thread title so others with this issue might find it useful

Edited by Earthshine
  • Like 1

My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
behdadsoft
Quote

can you post your code fixed code and mark this thread as Solved? you can edit the first post and add Solved to your thread title so others with this issue might find it useful

sure, I only moved this code :

; Create InputBox
local $Input = GUICtrlCreateInput("",100,25)
Local $idInputChange = GUICtrlCreateDummy()

to behind of this part:

;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()

finally:

;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()

; Create InputBox
local $Input = GUICtrlCreateInput("",100,25)
Local $idInputChange = GUICtrlCreateDummy()

 

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  

×