check if button is clicked

tripes

Member
Joined
Apr 16, 2009
Messages
18
Programming Experience
Beginner
how to check if a command button is clicked in vb.net 2003?
 
how to check if a command button is clicked in vb.net 2003?
Buttons in all versions of vb (well vb3 and up) including vb 7.1 (vs 2003) all have a click event, there isn't a property that you can check to tell if a button's been clicked.
 
At the form level you can create a boolean variable and set it to false. In your buttons click event you can set the variable to true when it first gets clicked. Later you can check whether this variable is equal to true or false.
 
You could use a static Boolean variable and turn it on or off. Here's a sample using a message box that the user can control:

VB.NET:
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Static clicked As Boolean
		Dim reply As Integer
		If clicked = True Then
			reply = MessageBox.Show("Click OK to keep clicked status on, Cancel to turn it off.", "This button was clicked", MessageBoxButtons.OKCancel)
		End If
		clicked = True
		If reply = 2 Then clicked = False
	End Sub
 
You could use a static Boolean variable and turn it on or off. Here's a sample using a message box that the user can control:

VB.NET:
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Static clicked As Boolean
		Dim reply As Integer
		If clicked = True Then
			reply = MessageBox.Show("Click OK to keep clicked status on, Cancel to turn it off.", "This button was clicked", MessageBoxButtons.OKCancel)
		End If
		clicked = True
		If reply = 2 Then clicked = False
	End Sub
That would only work if all of the coded needed to check that boolean remained inside the click event (that boolean doesn't have scope outside of that sub) which means there's no point in using the boolean in the first place.

If you're going to use the boolean approach (which I can't think of a valid reason why you would want to in the first place) you'll want a class (form) level boolean, toggle it in the click event then check it in whatever other sub/event where that knowledge is needed. Here's an example of toggling it:
VB.NET:
	Private m_ButtonClickBool As Boolean = False

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		m_ButtonClickBool = Not m_ButtonClickBool
	End Sub
 
Back
Top