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.