Jump to content

Recommended Posts

Posted (edited)

Hello Brothers , After pervois post I ended with what I was want , now my problem is to keep my program running after finishing search
I tried to Add

While 1

sleep ( 500)

wend

But it didn't work 

#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <File.au3>
Local $hGui = GUICreate("Example")
$ListviewBannerDate = GUICtrlCreateListView("My Banner Dates", 200, 150, 160, 150, -1)

; dummy control actioned by notify routine when user clicks on control
$dummy_lv = GUICtrlCreateDummy()

GUISetState(@SW_SHOW)

; routine to recieve notification messages from listview
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

Local $sFilePath = @ScriptDir & "\test.txt"
; Read the current script file into an array using the filepath.
Local $aDate = FileReadToArray($sFilePath)
If @error Then
    MsgBox(0, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file.
Else
    For $i = 0 To UBound($aDate) - 1 ; Loop through the array.
        $iString = StringInStr($aDate[$i], "OPTION VALUE")
        $sMyString = StringMid($aDate[$i], 16, 9)
        If $iString > 1 Then
            GUICtrlCreateListViewItem($sMyString, $ListviewBannerDate)
        Else
            ContinueLoop
        EndIf
    Next
EndIf

Local $msg

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $dummy_lv
            ;
            ; this is where you would do your search and replace routine
  $asd=  _GUICtrlListView_GetItemTextString($ListviewBannerDate, -1)        ;
If $asd ="" Then
    MsgBox(0,"sdfsd","")
    Else
        
           ConsoleWrite(_GUICtrlListView_GetItemTextString($ListviewBannerDate, -1) & @LF)
        ExitLoop
EndIf
    EndSwitch
WEnd

Local $sFileName = "test.txt"
Local $searchStr = $asd

$aResult = _LineNumsOfSearchStr($sFileName, $searchStr, False)

;_ArrayDisplay($aResult)

; Returns array of found line numbers that contain $searchStr contents.
; Optional delete found line from file.
Func _LineNumsOfSearchStr($sFileName, $searchString, $bDeleteLine = False)
    Local $location, $aCurrentLineNum, $iCurrentLineNum, $sFile, $iOccur = 1, $sRes = ""
    If FileExists($sFileName) = 0 Then Return 1
    Do
        $sFile = FileRead($sFileName)
        $location = StringInStr($sFile, $searchString, 0, $iOccur) ; Find the $iOccur occurrence of the "substring"

        If $location > 0 Then
            $aCurrentLineNum = StringRegExp(StringRegExpReplace($sFile, "(?s)(.{" & $location & "})(.*)$", "\1"), "(?s)(\v+)", 3) ;Find line number
            $iCurrentLineNum = UBound($aCurrentLineNum) + 1 ; Line number
MsgBox(0,"",$searchStr)
MsgBox(0,"",$iCurrentLineNum)
_FileWriteToLine(@ScriptDir&"\test.txt", $iCurrentLineNum+1, "my replacement for line 3", 1)
            ;ConsoleWrite("CharPos: " &  $location & "   Ln: " & $iCurrentLineNum & @CRLF)
            $sRes &= $iCurrentLineNum & "|"
            If $bDeleteLine Then
                _FileWriteToLine($sFileName, $iCurrentLineNum, "", 1) ; Remove found line from file.

            Else
                $iOccur += 1
            EndIf
        Else
            ExitLoop
        EndIf
        Sleep(10)
    Until 0
    ;ShellExecute($sFileName)
    Return StringSplit(StringTrimRight($sRes, 1), "|")
EndFunc   ;==>_LineNumsOfSearchStr
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)

    Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)

    Switch $tNMHDR.IDFrom
        Case $ListviewBannerDate
            Switch $tNMHDR.Code
                Case $nm_click
                    GUICtrlSendToDummy($dummy_lv) ; action dummy control in the message loop
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_NOTIFY

Test.txt :

<TR>
<TD CLASS="dddefault">
<SELECT NAME="selected_day">
<OPTION VALUE="14-OCT-16">14-OCT-16
<OPTION VALUE="13-OCT-16">14-OCT-16
<OPTION VALUE="12-OCT-16">14-OCT-16
<OPTION VALUE="11-OCT-16">14-OCT-16
</TR>

What I want :

To Just keep program Running WM_NOTIFY and start from began ..

 

Solved : Ty

@kylomas

@j0kky

 

 

Edited by abdulrahmanok
Posted (edited)

Sorry Guys I don't know why but program still closing after finishing search for selected Item !!!!

maybe problem in this func :

_LineNumsOfSearchStr($sFileName, $searchString, $bDeleteLine = False)

 

Edited by abdulrahmanok
Posted (edited)

abdulrahmanok,

As j0kky said, this will keep you in the messageloop...

#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <File.au3>
Local $hGui = GUICreate("Example")
$ListviewBannerDate = GUICtrlCreateListView("My Banner Dates", 200, 150, 160, 150, -1)

; dummy control actioned by notify routine when user clicks on control
$dummy_lv = GUICtrlCreateDummy()

GUISetState(@SW_SHOW)

; routine to recieve notification messages from listview
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

Local $sFilePath = @ScriptDir & "\test.txt"
; Read the current script file into an array using the filepath.
Local $aDate = FileReadToArray($sFilePath)
If @error Then
    MsgBox(0, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file.
Else
    For $i = 0 To UBound($aDate) - 1 ; Loop through the array.
        $iString = StringInStr($aDate[$i], "OPTION VALUE")
        $sMyString = StringMid($aDate[$i], 16, 9)
        If $iString > 1 Then
            GUICtrlCreateListViewItem($sMyString, $ListviewBannerDate)
        Else
            ContinueLoop
        EndIf
    Next
EndIf

Local $msg

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $dummy_lv
            ;
            ; this is where you would do your search and replace routine
            $aResult = _LineNumsOfSearchStr($sFilePath, _GUICtrlListView_GetItemTextString($ListviewBannerDate, -1), False)
            _ArrayDisplay($aResult)

            ;
            ;
    EndSwitch
WEnd

;Local $sFileName = "test.txt"   ;  <<<---- already declared in $sFilePath
;Local $searchStr = $asd

;$aResult = _LineNumsOfSearchStr($sFileName, $searchStr, False)  <<--------- put inside msgloop

;_ArrayDisplay($aResult)

; Returns array of found line numbers that contain $searchStr contents.
; Optional delete found line from file.
Func _LineNumsOfSearchStr($sFileName, $searchString, $bDeleteLine = False)
    Local $location, $aCurrentLineNum, $iCurrentLineNum, $sFile, $iOccur = 1, $sRes = ""
    If FileExists($sFilePath) = 0 Then Return 1
    Do
        $sFile = FileRead($sFilePath)
        $location = StringInStr($sFile, $searchString, 0, $iOccur) ; Find the $iOccur occurrence of the "substring"

        If $location > 0 Then
            $aCurrentLineNum = StringRegExp(StringRegExpReplace($sFile, "(?s)(.{" & $location & "})(.*)$", "\1"), "(?s)(\v+)", 3) ;Find line number
            $iCurrentLineNum = UBound($aCurrentLineNum) + 1 ; Line number
MsgBox(0,"",$searchString)
MsgBox(0,"",$iCurrentLineNum)
_FileWriteToLine(@ScriptDir&"\test.txt", $iCurrentLineNum+1, "my replacement for line 3", 1)
            ;ConsoleWrite("CharPos: " &  $location & "   Ln: " & $iCurrentLineNum & @CRLF)
            $sRes &= $iCurrentLineNum & "|"
            If $bDeleteLine Then
                _FileWriteToLine($sFileName, $iCurrentLineNum, "", 1) ; Remove found line from file.

            Else
                $iOccur += 1
            EndIf
        Else
            ExitLoop
        EndIf
        Sleep(10)
    Until 0
    ;ShellExecute($sFileName)
    Return StringSplit(StringTrimRight($sRes, 1), "|")
EndFunc   ;==>_LineNumsOfSearchStr
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)

    Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)

    Switch $tNMHDR.IDFrom
        Case $ListviewBannerDate
            Switch $tNMHDR.Code
                Case $nm_click
                    GUICtrlSendToDummy($dummy_lv) ; action dummy control in the message loop
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_NOTIFY

However, I think there are problems with how you are managing "test.txt".  And, you will want to maintain the listview in sync with that file (delete corresponding listview entry to test.txt entry).

kylomas

Edit: I would strongly advise you to review the help file and wiki doc for message mode processing.

Edited by kylomas
additional info

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Posted

@kylomas Thank You Very much that's exactly what I want , But Can you Explain me something :

What Is the difference between my old code :

$aResult = _LineNumsOfSearchStr($sFileName, $searchStr, False)

And Your Code :

$aResult = _LineNumsOfSearchStr($sFilePath, _GUICtrlListView_GetItemTextString($ListviewBannerDate, -1), False)

 

 

 

Posted

abdulrahmanok,

I just eliminated the interim variable $searchStr by using the result of the function call.  I hope you took some time to understand what is going on with message loop  processing.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...