Jump to content

[Solved] StringInstr() can't find all string


Recommended Posts

Hi.

I have this code that have a TreeView object with many parent and child. I want when write something into inputbox, compare input text with all child and if result is true show me matches child with input text. but i don't know why it don't work correctly and don't show me all matches child.

#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[1]
Dim $Child[10]
local $Count = 0
Global Const $SourcesPath = @ScriptDir

;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)
Local $hTreeObject = GUICtrlGetHandle($TreeObject)

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

;Redim $TreeParent UBound
ReDim $Parent[UBound($GetFolderList)]

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

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

GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")

while 1

   Switch GUIGetMsg()
      ; Search in InputBox
      Case $idInputChange
         _Search()

      Case $GUI_EVENT_CLOSE
         ExitLoop
   EndSwitch

WEnd

GUIDelete($GUI)

Func _Search()
   $InputText = GUICtrlRead($Input)
   ;Local $sParent[10], $aMatches[10], $iMatches = 0, $sMatchChild[10]
   if $InputText <> "" Then
      for $i = 0 to UBound($Child) - 1
         if StringInStr(GUICtrlRead($Child[$i],1), $InputText) <> 0 Then
            MsgBox(0,"",GUICtrlRead($Child[$i],1))
         EndIf
      Next
   EndIf
EndFunc

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

dose anyone know why do this happen?

Edited by behdadsoft
Link to comment
Share on other sites

Because the child items in $Child array are overwritten all the time:

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

Since you start with $b = 1 for each $a-loop all previous childs are overwritten:

for $b = 1 to UBound($RecordChildFolder) - 1
  $Child[$b] = GUICtrlCreateTreeViewItem($RecordChildFolder[$b] ,$Parent[$a])
Next

There was a reason why I changed this part of the code.

Edited by LarsJ
Link to comment
Share on other sites

  • Moderators

behdadsoft,

You are constantly reusing the same $Child array to list the contents of each folder - so you only ever end up with the final folder contents in that array. Not surprising that you do not find the contents of the other folders. You also limit this array to 10 elements - what happens if there are more than 10 folders within a folder?

You need to store all of the child ControlIDs - like this:

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

;Local $Count = 0 ; What is this for? <<<<<<<<<<<<<<<<<<<<<<
Global Const $SourcesPath = @ScriptDir

;Create GUI
Local $GUI = GUICreate("TreeView")

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

; Crete TreeView
Local $TreeObject = GUICtrlCreateTreeView(100, 70, 200, 250)
Local $hTreeObject = GUICtrlGetHandle($TreeObject)

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

;Create parent and child folders - child is a 2D array to hold all the child ControlIDs <<<<<<<<<<<<<<<<<<<
Local $Parent[UBound($GetFolderList)]
Local $Child[UBound($GetFolderList)][1]

;Get SubFolders and Add Child to Tree View Item
;Local $iStart = GUICtrlCreateDummy() : Why are you doing this? <<<<<<<<<<<<<<<<<<<
For $a = 1 To UBound($Parent) - 1
    $Parent[$a] = GUICtrlCreateTreeViewItem($GetFolderList[$a], $TreeObject)
    ; Note you need to add the "\" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    $RecordChildFolder = _FileListToArrayRec($SourcesPath & "\" & $GetFolderList[$a], "*", $FLTAR_FOLDERS, $FLTAR_NORECUR, $FLTAR_SORT)
    ; if folders were found <<<<<<<<<<<<<<<<<<<<<<<
    If Not @error Then
        ; Adjust the size of the child array to fit <<<<<<<<<<<<<<<<<<<<<<<<<
        If $RecordChildFolder[0] > UBound($Child, 2) - 1 Then
            ReDim $Child[UBound($GetFolderList)][$RecordChildFolder[0] + 1]
        EndIf
        For $b = 1 To UBound($RecordChildFolder) - 1
            $Child[$a][$b] = GUICtrlCreateTreeViewItem($RecordChildFolder[$b], $Parent[$a])
        Next
    EndIf

Next
;Local $iEnd = GUICtrlCreateDummy()

GUISetState()

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

GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")

While 1

    Switch GUIGetMsg()
        ; Search in InputBox
        Case $idInputChange
            _Search()

        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch

WEnd

GUIDelete($GUI)

Func _Search()
    $InputText = GUICtrlRead($Input)
    If $InputText <> "" Then
        ; Now look through the 2D array to find matches
        For $i = 0 To UBound($Child) - 1
            For $j = 0 To UBound($Child, 2) - 1
                $TreeItemText = GUICtrlRead($Child[$i][$j], 1)
                If StringInStr($TreeItemText, $InputText) <> 0 Then
                    MsgBox(0, "", $TreeItemText)
                EndIf
            Next
        Next
    EndIf
EndFunc   ;==>_Search

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

That works fine for me.

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

is your mean this part?

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

This piece of code don't work with my above code (First post).

Edited by Melba23
Quote removed
Link to comment
Share on other sites

  • Moderators

behdadsoft,

If it works as it is, why do you need to rewrite it to use a 1D array?

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

  • Moderators

behadsoft,

Only a little different:

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

Local $Count = 0 ; Now you need this <<<<<<<<<<<<<<<<<<<<<<
Global Const $SourcesPath = @ScriptDir

;Create GUI
Local $GUI = GUICreate("TreeView")

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

; Crete TreeView
Local $TreeObject = GUICtrlCreateTreeView(100, 70, 200, 250)
Local $hTreeObject = GUICtrlGetHandle($TreeObject)

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

;Create parent and child folders - child is a 1D array to hold all the child ControlIDs <<<<<<<<<<<<<<<<<<<
Local $Parent[UBound($GetFolderList)]
Local $Child[1]

;Get SubFolders and Add Child to Tree View Item
For $a = 1 To UBound($Parent) - 1
    $Parent[$a] = GUICtrlCreateTreeViewItem($GetFolderList[$a], $TreeObject)
    ; Note you need to add the "\" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    $RecordChildFolder = _FileListToArrayRec($SourcesPath & "\" & $GetFolderList[$a], "*", $FLTAR_FOLDERS, $FLTAR_NORECUR, $FLTAR_SORT)
    ; if folders were found <<<<<<<<<<<<<<<<<<<<<<<
    If Not @error Then
        ; Adjust the size of the child array to fit the new children <<<<<<<<<<<<<<<<<<<<<<<<<
        ReDim $Child[UBound($Child) + $RecordChildFolder[0]]
        For $b = 1 To UBound($RecordChildFolder) - 1
            ; Increase count to get next insertion point
            $Count += 1
            $Child[$Count] = GUICtrlCreateTreeViewItem($RecordChildFolder[$b], $Parent[$a])
        Next
    EndIf

Next
;Local $iEnd = GUICtrlCreateDummy()

GUISetState()

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

GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")

While 1

    Switch GUIGetMsg()
        ; Search in InputBox
        Case $idInputChange
            _Search()

        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch

WEnd

GUIDelete($GUI)

Func _Search()
    $InputText = GUICtrlRead($Input)
    If $InputText <> "" Then
        ; Now look through the 2D array to find matches
        For $i = 1 To UBound($Child) - 1
            $TreeItemText = GUICtrlRead($Child[$i], 1)
            If StringInStr($TreeItemText, $InputText) <> 0 Then
                MsgBox(0, "", $TreeItemText)
            EndIf
        Next
    EndIf
EndFunc   ;==>_Search

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

And please when you reply in future, use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - responders know what they wrote and it just pads the thread unnecessarily. Thanks in advance for your cooperation.

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

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