How do i track button presses

Im a building a powwershell form that triggers relays to turn on and off and wanted to keep track of which buttons are used and many times each one has been pressed.

1 answer

  • answered 2021-10-24 15:14 Mathias R. Jessen

    You need to attach event action blocks to the buttons' Click events, increment your counters there:

    # Create a hashtable to store the button counters at the start of the script
    $buttonClickCounters = @{}
    
    # define form components here etc...
    
    # Assuming $button1 contains a System.Windows.Forms.Button:
    $button1.add_Click({
      # someone clicked the button, increment counter
      $buttonClickCounters['button1']++
    
      # implement whatever the button is actually supposed to affect down here
    })
    

    Once the form has run, you can inspect the individual number of button clicks by inspecting the values stored in the hashtable:

    $buttonClickCounters['button1'] -as [int] # this expression will now resolve to the number of times the first button was clicked
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum