Jump to content
triodz

Have number increase each time a button is pressed

Recommended Posts

triodz

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! 

Share this post


Link to post
Share on other sites
jguinch

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

 

Share this post


Link to post
Share on other sites
Malkey

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

 

Share this post


Link to post
Share on other sites
triodz
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!)

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

  • Similar Content

    • XinYoung
      By XinYoung
      For fun, I'm building an app that opens a webpage and refreshes it every 30 seconds.
      But once the script performs _IEAction($oIE, "refresh"), the GUI closes.
      Any help is appreciated.
       

      #include <ButtonConstants.au3>
      #include <EditConstants.au3>
      #include <GUIConstantsEx.au3>
      #include <StaticConstants.au3>
      #include <WindowsConstants.au3>
      #include <WinAPIFiles.au3>
      #include <Array.au3>
      #include <File.au3>
      #include <Excel.au3>
      #include <DateTimeConstants.au3>
      #include <MsgBoxConstants.au3>
      #include <WinAPIShellEx.au3>
      #include <MsgBoxConstants.au3>
      #include <Date.au3>
      #include <ComboConstants.au3>
      #include <guimenu.au3>
      #include <IE.au3>
      HotKeySet("{F4}", "_Exit")
      ;Open the file(s) in the selected folder
      $extension = ".txt"
      $app2openWith = @SystemDir & "\notepad.exe"
      Func Begin()
          Global $loopTrick = 0
          #Region ### START Koda GUI section ### Form=c:\users\mchu\downloads\autoit\my code\form1.kxf
          Global $UI = GUICreate("Hit Em Up!", 256, 113, -1, -1)
          GUISetBkColor(0x000000)
          $menu = _GUICtrlMenu_GetSystemMenu($UI)
          _GUICtrlMenu_EnableMenuItem($menu, $SC_CLOSE, 1, False)
          Global $url = GUICtrlCreateInput("https://www.youtube.com/watch?v=dQw4w9WgXcQ", 81, 8, 160, 21)
          $Label1 = GUICtrlCreateLabel("Target:", 16, 8, 55, 17)
          GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
          GUICtrlSetColor(-1, 0x00FF00)
          Global $StartBut = GUICtrlCreateButton("Start", 16, 40, 67, 25)
          GUICtrlSetFont(-1, 13, 800, 0, "MS Sans Serif")
          GUICtrlSetBkColor(-1, 0x008000)
          $Label2 = GUICtrlCreateLabel("(Press F4 to Exit)", 96, 40, 8000, 17)
          GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
          GUICtrlSetColor(-1, 0x00FF00)
          GUISetState(@SW_SHOW)
          #EndRegion ### END Koda GUI section ###
          While 1
              $UIfunc = GUIGetMsg()
              Select
                  Case $UIfunc = $GUI_EVENT_CLOSE
                      _Exit()
                  Case $UIfunc = $StartBut
                      If GUICtrlRead($url) = "" Then
                          MsgBox(48, "Um...", "Give me a target you idiot.")
                      Else
                          GUICtrlSetState($url, $GUI_DISABLE)
                          GUICtrlSetState($StartBut, $GUI_DISABLE)
                          Start()
                      EndIf
              EndSelect
          WEnd
      EndFunc   ;==>Begin
      Func Start()
          If $loopTrick = 0 Then
              Global $oIE = _IECreate(GUICtrlRead($url))
              _IELoadWait($oIE)
              Again()
          ElseIf $loopTrick = 1 Then
              Sleep(3000)
              _IEAction($oIE, "refresh")
              Sleep(3000)
              Start()
          EndIf
      EndFunc   ;==>Start
      Func Again()
          $loopTrick = 1
          Start()
      EndFunc   ;==>Again
      Func _Exit()
          Exit
      EndFunc   ;==>_Exit
    • kcvinu
      By kcvinu
      Hi all,
      I have a button created with _GUICtrlButton_Create function. I know that this function returns a handle of button instead of an ID. So i have obtain control ID by calling _WinAPI_GetDlgCtrlID function. After that i have tried the GUICtrlSetBkColor function. But it didnt worked. Any suggestions will be appreciated. Thanks in advance. 
      Note : - The window which carries this button is not created with native gui create function. Instead, it is created with CreateWindowEx api function.
    • kcvinu
      By kcvinu
      Hi all,
      I am playing with _GUICtrlButton_Create function. How can i change this button's (or the entire form's) font ?. The in-built GUICtrlSetFont function is not working even when i convert the control handle to control ID with _WinAPI_GetDlgCtrlID ( ) function.  Do i need to use CreateFont api finction and send WM_SETFONT message ? Or is there any other easy and safe ways to do this ?. Thanks in advance.
      Note : This window is created by CreateWindowEx function, not by GUICreate function. 
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.