The Boolean variable in an event will only work once. As soon as the event ends, it will go back to the original value, since it is no longer in memory. What you need to do is declare the variable as Static. That way it will remain in memory and you can use the button as a toggle along with the logical Not operator. Here is an example:
Code:
Private Sub btnToggle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToggle.Click
Static onoff As Boolean
onoff = Not onoff
If onoff = True Then
MessageBox.Show("ON")
Else
MessageBox.Show("OFF")
End If
End Sub However, if you only want the code to work once, then there is no need for the Static declaration or the Not toggle. The default of the Boolean variable is False, and when the button is clicked, change it to True.
Bookmarks