The issue lies in the fact that the Wait For statement is consuming the button click event before it can be handled by the Button_Click subroutine. This is why the code in Button_Click is not executed, and the ClickedButton variable remains empty.
To resolve this issue, you can modify the code by introducing a flag variable to track whether a button click has occurred. look here
Sub MainModule
Dim ButtonClicked As Boolean = False ' Flag variable to track button clicks
Do While True
If ButtonClicked Then
Log("You clicked " & ClickedButton)
ButtonClicked = False ' Reset the flag
End If
Wait For Button_Click
' Button_Click event will set the flag and assign the clicked button text
Loop
End Sub
Sub Button_Click
Dim btn As Button = Sender
ClickedButton = btn.Text
ButtonClicked = True ' Set the flag to indicate a button click
End Sub
By introducing the ButtonClicked flag, you can check if a button click has occurred inside the loop. If a button click has been registered, you can log the clicked button's text and reset the flag. This ensures that the code in Button_Click is executed and the ClickedButton variable is updated accordingly.