triodz Posted February 5, 2017 Posted February 5, 2017 I want the number in a file to be overwritten with the next number up each time a button is pressed. So it is pressed once, the number '1' is written to the file. It is pressed again and the number '2' overwrites the first number. At the moment I get the number 1 written, but can't get it to overwrite. What I have so far: #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> #Region ### START Koda GUI section ### Form=C:\Users\soulf\Desktop\CalcGui\RLCalc.kxf $Form1 = GUICreate("Form1", 615, 437, 426, 141) $One = GUICtrlCreateButton("1", 32, 72, 25, 25) GUICtrlSetBkColor(-1, 0xFF0000) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Func First () $fPath = "C:\temp\First.txt" $fOpen = FileOpen ($fPath, 2) $Counter = FileReadLine ($fpath) $counter = $counter + 1 $fWrite = filewriteline ($fPath, $counter) FileClose ($fpath) EndFunc While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $One Call ("First") EndSwitch WEnd I tried a Do Until statement, but that just puts the numbers 1 to 5 in the file. Any help would be greatly appreciated!
jguinch Posted February 5, 2017 Posted February 5, 2017 Declare $counter as global variable or local Static : $Form1 = GUICreate("Form1", 615, 437, 426, 141) $One = GUICtrlCreateButton("1", 32, 72, 25, 25) GUICtrlSetBkColor(-1, 0xFF0000) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Func First () Local Static $counter = 0 $counter += 1 $fPath = "C:\temp\First.txt" $fOpen = FileOpen ($fPath, 2) $Counter = FileReadLine ($fpath) $fWrite = filewriteline ($fPath, $counter) FileClose ($fpath) EndFunc While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $One ; Call ("First") <---- Call not needed First() EndSwitch WEnd Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
Malkey Posted February 5, 2017 Posted February 5, 2017 Note you haven't used the file handle from FileOpen function in the FileClose function. #include <GUIConstantsEx.au3> Global $fPath = "First.txt" ; This file will be created in the script's directory (no path). Local $Form1 = GUICreate("Form1", 615, 437, 426, 141) Local $One = GUICtrlCreateButton("0", 32, 72, 25, 25) GUICtrlSetBkColor(-1, 0xFF0000) If FileExists($fPath) Then GUICtrlSetData($One, FileRead($fPath)) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $One _First() EndSwitch WEnd Func _First() Local $Counter = FileRead($fPath) $Counter = $Counter + 1 Local $fOpen = FileOpen($fPath, 2) ; $FO_OVERWRITE (2) = Write mode (erase previous contents) FileWrite($fOpen, $Counter) ; Use file handle write mode. FileClose($fOpen) GUICtrlSetData($One, $Counter) ; Update the text on button. EndFunc ;==>_First
triodz Posted February 6, 2017 Author Posted February 6, 2017 20 hours ago, jguinch said: Declare $counter as global variable or local Static : $Form1 = GUICreate("Form1", 615, 437, 426, 141) $One = GUICtrlCreateButton("1", 32, 72, 25, 25) GUICtrlSetBkColor(-1, 0xFF0000) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Func First () Local Static $counter = 0 $counter += 1 $fPath = "C:\temp\First.txt" $fOpen = FileOpen ($fPath, 2) $Counter = FileReadLine ($fpath) $fWrite = filewriteline ($fPath, $counter) FileClose ($fpath) EndFunc While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $One ; Call ("First") <---- Call not needed First() EndSwitch WEnd Thanks for the help with this one, but it unfortunately it just created a blank file. 19 hours ago, Malkey said: Note you haven't used the file handle from FileOpen function in the FileClose function. #include <GUIConstantsEx.au3> Global $fPath = "First.txt" ; This file will be created in the script's directory (no path). Local $Form1 = GUICreate("Form1", 615, 437, 426, 141) Local $One = GUICtrlCreateButton("0", 32, 72, 25, 25) GUICtrlSetBkColor(-1, 0xFF0000) If FileExists($fPath) Then GUICtrlSetData($One, FileRead($fPath)) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $One _First() EndSwitch WEnd Func _First() Local $Counter = FileRead($fPath) $Counter = $Counter + 1 Local $fOpen = FileOpen($fPath, 2) ; $FO_OVERWRITE (2) = Write mode (erase previous contents) FileWrite($fOpen, $Counter) ; Use file handle write mode. FileClose($fOpen) GUICtrlSetData($One, $Counter) ; Update the text on button. EndFunc ;==>_First This worked perfectly! Thanks so much for your help. You actually went above and beyond and got me on my way with my next challenge of having the number on the button update (well it will be on a label, but same-same!)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now